Search code examples
c#arraysunity-game-enginetexturestexture2d

Unity create texture from TGA


I try to generate sprites from StreamingAsets folder but I'm stuck when it comes to create texture from formats other than PNG and JPG. Following code:

    byte[] bytes = File.ReadAllBytes (filepath);    // 256x256 .tga image file
    Texture2D texture = new Texture2D (1, 1);
    texture.LoadImage (bytes);

generates 8x8 texture which is:

  • wrong considered my image is 256x256,
  • expected, since Texture2D.LoadImage is meant to work with PNGs and JPGs.

So how do I create textures from other types of images?


Solution

  • TGA format is not supported. You have to write your own wrapper to do so but many of these wrappers do exist already. See the TGALoader class. It is very simple to use.

    Texture2D texture = TGALoader.LoadTGA(sfilepath);
    

    Your TGA image is now loaded into the texture variable.