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 小时前
Unity3D 编辑器对象锁定工具
unity·编辑器·编程·工具·对象·互动·拓展
CG_MAGIC5 小时前
Enscape 新手从零到出图全流程
3d·贴图·uv·建模教程·渲云渲染
AI前沿资讯5 小时前
一站式 AI 3D 创作首选:V2Fun—— 直连 Unity + 多人动捕双核心,重塑轻量化生产管线
人工智能·3d·unity
winlife_15 小时前
Unity 域重载会清空一切:Editor 工具如何让状态在重载后续命
unity·游戏引擎
小贺儿开发17 小时前
Unity3D 串口通信上位机联调系统
unity·串口·协议·数据·通信·传输·互动
摄影图1 天前
量子计算科技图片素材 高清商用适配多场景
科技·aigc·量子计算·贴图·插画
垂葛酒肝汤2 天前
Unity的UI扫光效果Shader
ui·unity·游戏引擎
mxwin2 天前
Unity Shader Alpha测试 · 模板测试 · 深度测试
unity·游戏引擎
Sator12 天前
unity解决粒子与物体接触时的硬边缘问题
unity·游戏引擎
CG_MAGIC2 天前
3ds Max 模型导入导出常见报错解决方法
3d·贴图·效果图·建模教程·渲云渲染