统一更改Texture2D格式

use*_*759 2 c# unity-game-engine

我有一个Textured2D加载,表示ETC_RGB4我如何将其更改为另一种格式?说RGBA32.基本上我想从3个通道切换到4个通道,每通道4个比特切换到每通道8个通道.

谢谢

Pro*_*mer 6

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

1 .Create新的空Texture2D并提供RGBA32TextureFormat说法.这将创建具有RGBA32格式的空纹理.

2.使用Texture2D.GetPixels以获取旧纹理在的像素ETC_RGB4格式,然后用Texture2D.SetPixels把新创建的纹理的像素从#1.

3 .CALL Texture2D.Apply应用更改.而已.

一个简单的扩展方法:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

public Texture2D theOldTextue;

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