unity 把Texture转成Texture2D

 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;
        }

你可能感兴趣的:(Unity开发)