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]Inspector各种写法(持续更新中)
java·unity·游戏引擎
毕竟秋山澪1 天前
unity Skill接入TraeAI操作步骤
unity·游戏引擎
XR-AI-JK1 天前
01-VR开发如何配置和搭建基础环境
unity·vr·vr基础教程·vr教程·vr实战教程·vr节奏游戏·unityvr教程
派葛穆1 天前
Unity-生成预制体1
unity
WarPigs1 天前
Unity CG着色器实战
unity·着色器
废嘉在线抓狂.1 天前
TimeLine如何自定义轨道
unity·c#·对话系统
ellis19702 天前
Unity资源管理框架Addressables[六] 内存管理
unity
派葛穆2 天前
Unity-鼠标悬停改变物体层级
unity·游戏引擎
小贺儿开发2 天前
Unity3D 爆炸图案例演示
unity·产品·urp·机械拆装·爆炸图·零件·效果设计
Yasin Chen3 天前
Unity TMP_SDF 分析(二)数据来源2
unity·游戏引擎