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
相关推荐
z2014z2 小时前
Unity Timeline
unity·游戏引擎
tealcwu2 小时前
【Unity基础】如何选择Mono的.Net API版本
unity·游戏引擎·.net
鹿野素材屋4 小时前
Unity3D类似于桌面精灵的功能实现
unity·c#·游戏引擎
Thomas_YXQ9 小时前
Unity3D 实现水体交互详解
开发语言·unity·编辑器·交互·unity3d·游戏开发
野奔在山外的猫19 小时前
【解决】AnimationCurve 运行时丢失数据问题
unity
念威21 小时前
unity导入半透明webm + AE合成半透明视频
unity·游戏引擎·音视频·web
阿赵3D1 天前
Unity制作角色溶解变成光点消失
java·unity·游戏引擎·shader·溶解·消失
仙魁XAN1 天前
Unity 之 【Android Unity FBO渲染】之 [Unity 渲染 Android 端播放的视频] 的一种方法简单整理
android·unity·共享纹理·fbo·视频渲染
折纸星空Unity课堂1 天前
003InputSystem新输入系统学习工作笔记
笔记·学习·游戏·unity
小张不爱写代码1 天前
Unity 使用Spine动画切换时有残影
unity·spine