Unity 贴图拷贝与性能对比

Cooooopy

  • GetPixels
  • GetRawTextureData
  • RenderTexture
  • Graphics.CopyTexture
  • 性能对比

GetPixels

 var pixels = tex.GetPixels();
 tex2.SetPixels(pixels);
 tex2.Apply();

GetRawTextureData

 var pixels = tex.GetRawTextureData();
 tex2.LoadRawTextureData(pixels);
 tex2.Apply();

RenderTexture

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!!!

Graphics.CopyTexture(tex1, tex2);

性能对比

在测试环境每帧调用 把Texture2D拷贝到另一张Texture2D上。

Method cpu (基于已有工程测试,只改变拷贝方法)
GetPixels 20.3ms
RenderTexture 7.9ms
GetRawTextureData 8.8ms
Graphics.CopyTexture 5.2ms

你可能感兴趣的:(Unity,unity,贴图)