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
相关推荐
ttod_qzstudio12 小时前
Unity中使用EzySlice实现模型切割与UV控制完全指南
unity
南無忘码至尊13 小时前
Unity 实现与 Ollama API 交互的实时流式响应处理
unity·游戏引擎·交互
平行云16 小时前
如何实现UE程序大并发多集群的像素流部署
unity·ue5·图形渲染
向宇it2 天前
【unity小技巧】在 Unity 中将 2D 精灵添加到 3D 游戏中,并实现阴影投射效果,实现类《八分旅人》《饥荒》等等的2.5D游戏效果
游戏·3d·unity·编辑器·游戏引擎·材质
向宇it2 天前
Unity Universal Render Pipeline/Lit光照材质介绍
游戏·unity·c#·游戏引擎·材质
__water2 天前
RHA《Unity兼容AndroidStudio打Apk包》
android·unity·jdk·游戏引擎·sdk·打包·androidstudio
两水先木示3 天前
【Unity3D】微信小游戏适配安全区域或胶囊控件(圆圈按钮)水平高度一致方案
unity·微信小游戏·安全区域·ui适配·胶囊控件·safearea
枯萎穿心攻击3 天前
ECS由浅入深第三节:进阶?System 的行为与复杂交互模式
开发语言·unity·c#·游戏引擎
不绝1913 天前
怪物机制分析(有限状态机、编辑器可视化、巡逻机制)
网络·游戏·unity·游戏引擎
unicrom_深圳市由你创科技3 天前
Unity开发如何解决iOS闪退问题
unity·ios·蓝桥杯