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
相关推荐
怣疯knight4 小时前
Cocos creator判断节点是否能用的方法
unity·cocos2d
tealcwu4 小时前
Google Play的Keystore不可用时的解决方法
unity
da_vinci_x4 小时前
PS 消失点:贴图透视总画歪?无需建模,2D 也能“空间绘图”
游戏·aigc·设计师·贴图·技术美术·游戏美术·关卡设计
呼呼突突4 小时前
Unity使用TouchSocket的RPC
unity·rpc·游戏引擎
qq 1808095119 小时前
从零构建一个多目标多传感器融合跟踪器
unity
平行云20 小时前
实时云渲染支持在网页上运行UE5开发的3A大作Lyra项目
unity·云原生·ue5·webgl·虚拟现实·实时云渲染·像素流送
鹏飞于天20 小时前
Shader compiler initialization error: Failed to read D3DCompiler DLL file
unity
wonder1357921 小时前
UGUI重建流程和优化
unity·游戏开发·ugui
UpYoung!1 天前
【截图与贴图工具推荐】截图与贴图工具——PixPin保姆级详细图文下载安装指南
图像处理·贴图·实用工具·图像编辑·截图工具·开源工具·运维必备
那个村的李富贵1 天前
Unity打包Webgl后 本地运行测试
unity·webgl