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
相关推荐
沉默金鱼1 小时前
Unity实用技能-GM命令
unity·游戏引擎
da_vinci_x1 小时前
【游戏场景】手绘贴图“接缝”地狱?PS 智能平铺流,3步量产无缝地砖
游戏·3d·prompt·aigc·贴图·技术美术·游戏美术
chillxiaohan2 小时前
unity粗糙、高光、光泽度调节shader记录
unity·游戏引擎
星夜泊客4 小时前
Unity UI 渲染与 Rebuild 机制简易解析
unity·游戏引擎
qiminixi15 小时前
Unity 6000下载
unity·unity 6000·unity 6000下载
CreasyChan19 小时前
Unity Shader 入门指南
unity·c#·游戏引擎·shader
漂视数字孪生世界20 小时前
Unity团结引擎的前世今生
unity·游戏引擎·数字孪生
心前阳光1 天前
Unity通过ScriptableObject学习访问者模式
学习·unity·访问者模式
fcm191 天前
unity之重新导入TMP
unity
心疼你的一切1 天前
【技术创作的璀璨盛宴——2025年CSDN博客之星总评选深度总结】
microsoft·unity·游戏引擎·游戏程序·csdn·博客之星