-
Notifications
You must be signed in to change notification settings - Fork 14
/
LoadTGA.cs
31 lines (30 loc) · 973 Bytes
/
LoadTGA.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.IO;
using UnityEngine;
public static class LoadTGA
{
// Loads 32-bit (RGBA) uncompressed TGA. Actually, due to TARGA file structure, BGRA32 is good option...
// Disabled mipmaps. Disabled read/write option, to release texture memory copy.
public static Texture2D Load(string fileName)
{
try
{
BinaryReader reader = new BinaryReader(File.OpenRead(fileName));
reader.BaseStream.Seek(12, SeekOrigin.Begin);
short width = reader.ReadInt16();
short height = reader.ReadInt16();
reader.BaseStream.Seek(2, SeekOrigin.Current);
byte[] source = reader.ReadBytes(width * height * 4);
reader.Close();
Texture2D texture = new Texture2D(width, height, TextureFormat.BGRA32, false);
texture.LoadRawTextureData(source);
texture.name = Path.GetFileName(fileName);
texture.Apply(false, true);
return texture;
}
catch (Exception)
{
return Texture2D.blackTexture;
}
}
}