首页 > 解决方案 > 统一更改 Texture2D 格式

问题描述

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

谢谢

标签: c#unity3d

解决方案


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

1 .创建新的空Texture2D并提供RGBA32TextureFormat参数。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);
}

推荐阅读