且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

统一更改Texture2D格式

更新时间:2023-11-09 23:04:22

您可以在运行时更改纹理格式.

You can change texture format during run-time.

1 .创建一个新的空Texture2D并为TextureFormat自变量提供RGBA32.这将创建RGBA32格式的空纹理.

1.Create new empty Texture2D and provide RGBA32 to the TextureFormat argument. This will create an empty texture with the RGBA32 format.

2 .使用Texture2D.GetPixels获取ETC_RGB4格式的旧纹理的像素,然后使用Texture2D.SetPixels将这些像素放入#1新创建的纹理中.

2.Use Texture2D.GetPixels to obtain the pixels of the old texture that's in ETC_RGB4 format then use Texture2D.SetPixels to put those pixels in the newly created Texture from #1.

3 .致电Texture2D.Apply应用更改.就是这样.

3.Call Texture2D.Apply to apply the changes. That's it.

一个简单的扩展方法:

public static class TextureHelperClass
{
    public static Texture2D ChangeFormat(this Texture2D oldTexture, TextureFormat newFormat)
    {
        //Create new empty Texture
        Texture2D newTex = new Texture2D(2, 2, newFormat, false);
        //Copy old texture pixels into new one
        newTex.SetPixels(oldTexture.GetPixels());
        //Apply
        newTex.Apply();

        return newTex;
    }
}

用法:

public Texture2D theOldTextue;

// Update is called once per frame
void Start()
{
    Texture2D RGBA32Texture = theOldTextue.ChangeFormat(TextureFormat.RGBA32);
}