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
相关推荐
在路上看风景9 小时前
## 2.2 状态同步
unity
霜绛10 小时前
Unity:lua热更新(一)——AB包AssetBundle、Lua语法
笔记·学习·游戏·unity·lua
霜绛10 小时前
Unity:lua热更新(二)——Lua语法(续)
笔记·学习·unity·游戏引擎·lua
yi碗汤园10 小时前
【一文了解】C#反射
开发语言·unity·c#
HahaGiver66611 小时前
Unity Shader Graph 3D 实例 - 基础的模型贴图渲染
3d·unity·游戏程序·贴图·游戏美术
HahaGiver66611 小时前
Unity Shader Graph 3D 实例 - 一个简单的3D打印效果
3d·unity·游戏引擎
胖胖求游戏16 小时前
Unity热更新——AB包和Lua
unity·游戏引擎·lua
为你写首诗ge17 小时前
【Unity知识分享】Unity中获取Pico设备的序列号(SN码)
unity
B0URNE20 小时前
什么是虚拟现实(VR)?
unity·ue5·vr
B0URNE1 天前
【Unity基础详解】Unity3D全程学习路线
学习·unity·游戏引擎