在Unity中更改Texture2D的格式

5

我有一个以ETC_RGB4格式表示的Textured2D,我该如何将其更改为其他格式?比如RGBA32。基本上我想从3通道切换到4通道,并且从每个通道的4位切换到8位。

谢谢


根据文档GetRawTextureData返回一个字节数组,而不是所提到的ETC_RBG4。这里有什么我没有理解的吗? - Swift
不太清楚和混乱,让我改变我的问题。 - user8469759
1个回答

11

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

1。创建一个新的空 Texture2D,并将RGBA32提供给TextureFormat参数。这将创建一个带有RGBA32格式的空纹理。

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

3。调用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;
    }
}

使用方法:

public Texture2D theOldTextue;

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接