Unity 贴图拷贝与性能对比

Cooooopy

🌳GetPixels

csharp 复制代码
 var pixels = tex.GetPixels();
 tex2.SetPixels(pixels);
 tex2.Apply();

🌳GetRawTextureData

csharp 复制代码
 var pixels = tex.GetRawTextureData();
 tex2.LoadRawTextureData(pixels);
 tex2.Apply();

🌳RenderTexture

csharp 复制代码
public static void textureToTexture2D(Texture texture, Texture2D texture2D)
{
    if (texture == null)
        throw new ArgumentNullException("texture");

    if (texture2D == null)
        throw new ArgumentNullException("texture2D");

    if (texture.width != texture2D.width || texture.height != texture2D.height)
        throw new ArgumentException("texture and texture2D need to be the same size.");

    RenderTexture prevRT = RenderTexture.active;

    if (texture is RenderTexture)
    {
        RenderTexture.active = (RenderTexture)texture;
        texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
        texture2D.Apply(false, false);
    }
    else
    {
        RenderTexture tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
        Graphics.Blit(texture, tempRT);

        RenderTexture.active = tempRT;
        texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
        texture2D.Apply(false, false);
        RenderTexture.ReleaseTemporary(tempRT);
    }

    RenderTexture.active = prevRT;
}

🌳Graphics.CopyTexture

不需要apply!!!

csharp 复制代码
Graphics.CopyTexture(tex1, tex2);

🌭性能对比

在测试环境每帧调用 把Texture2D拷贝到另一张Texture2D上。

Method cpu (基于已有工程测试,只改变拷贝方法)
GetPixels 20.3ms
RenderTexture 7.9ms
GetRawTextureData 8.8ms
Graphics.CopyTexture 5.2ms
相关推荐
点量云实时渲染-小芹7 小时前
UE/Unity/Webgl云渲染推流网址,如何与外部网页嵌套和交互?
unity·webgl·webgl云渲染网页交互·点量云流
ynxw7 小时前
Unity AR 实现模型交互
android·unity·ar·交互
龚子亦3 天前
【Shader学习】完整光照效果
unity·技术美术·shader
weixin_531638943 天前
Rokid AR交互开发工具对比
unity·游戏引擎·xr
留待舞人归3 天前
【Unity3D优化】优化多语言字体包大小
游戏·unity·游戏引擎·unity3d·优化
9765033354 天前
iOS 审核 cocos 4.3a【苹果机审的“分层阈值”设计】
flutter·游戏·unity·ios
EQ-雪梨蛋花汤4 天前
【Unity笔记】Unity Animation组件使用详解:Play方法重载与动画播放控制
笔记·unity·游戏引擎
AgilityBaby4 天前
Untiy打包安卓踩坑
android·笔记·学习·unity·游戏引擎
菌菌巧乐兹5 天前
Unity | AmplifyShaderEditor插件基础(第九集:旗子进阶版)
unity·游戏引擎
心前阳光5 天前
Unity编辑器-获取Projectwindow中拖拽内容的路径
unity·编辑器·游戏引擎