Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Texture2D format in unity

Tags:

c#

unity3d

I have a Textured2D loaded which is represented in ETC_RGB4 how can I change this to another format? say RGBA32. Basically I want to switch from 3 channels to 4 and from 4 bit per channel to 8 per channel.

Thanks

like image 365
user8469759 Avatar asked Jan 28 '23 05:01

user8469759


1 Answers

You can change texture format during run-time.

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

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.Call Texture2D.Apply to apply the changes. That's it.

A simple extension method for this:

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

USAGE:

public Texture2D theOldTextue;

// Update is called once per frame
void Start()
{
    Texture2D RGBA32Texture = theOldTextue.ChangeFormat(TextureFormat.RGBA32);
}
like image 158
Programmer Avatar answered Jan 31 '23 20:01

Programmer