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
相关推荐
gzroy21 小时前
Unity Shader Graph实现全息瞄准器
unity·游戏引擎
90后小陈老师1 天前
Unity教学 基础介绍
unity·游戏引擎
90后小陈老师1 天前
Unity教学 项目3 3D坦克大战
3d·unity·游戏引擎
秦奈1 天前
Unity复习学习随笔(五):Unity基础
学习·unity·游戏引擎
nnsix1 天前
Unity ReferenceFinder插件 窗口中选择资源时 同步选择Assets下的资源
java·unity·游戏引擎
麷飞花1 天前
unity3d scene窗口选中物体, 在 hierarchy高光显示
unity·editor·unity3d·u3d·hierarchy
ۓ明哲ڪ1 天前
Unity功能——关闭脚本自动编译(Unity2021.3)
unity·游戏引擎
90后小陈老师1 天前
Unity教学 项目4 3D求生枪手
3d·unity·游戏引擎
CreasyChan2 天前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
世洋Blog2 天前
SiYangUnityEventSystem,一个Unity中的事件系统
观察者模式·unity·c#·游戏引擎·事件系统