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
相关推荐
在路上看风景5 小时前
1.11 资源划分策略
unity
红黑色的圣西罗8 小时前
Unity减少Draw和SetPassCall的手段简述
unity·游戏引擎
淡海水10 小时前
【节点】[CorneaRefraction节点]原理解析与实际应用
unity·游戏引擎·shadergraph·图形·cornea·refraction
njsgcs10 小时前
blender导出fbx没有贴图问题
blender·贴图
小贺儿开发10 小时前
《唐朝诡事录之西行》——降魔变
科技·unity·人机交互·创意·文旅·hdrp
泡泡茶壶ᐇ11 小时前
Unity游戏开发入门指南:从零开始理解游戏引擎核心概念(二:Unity游戏开发的本质:从可视化编程到面向对象的顿悟)
unity·游戏引擎
小贺儿开发11 小时前
Unity3D 汽车视界(轻量版)
科技·unity·汽车·人机交互·可视化·urp
在路上看风景1 天前
32. 代码优化
unity
在路上看风景1 天前
01. 编辑器外观
unity
CreasyChan1 天前
unity C# 实现屏蔽敏感词
unity·c#·游戏引擎