Skip to content

Decoding Texture2Ds

nesrak1 edited this page Sep 23, 2023 · 2 revisions

Assuming you've installed the Texture nuget plugin, and some kind of image library like ImageSharp or System.Drawing, extracting textures is decently easy. Since it's open to any image library, it's a little more complicated than just "save to png", but it's easier than doing it all by hand. The decoder code is provided by AssetRipper's texture decoder library.

var am = new AssetsManager();

am.LoadClassPackage("classdata.tpk");
var fileInst = am.LoadAssetsFile("sharedassets0.assets", false);
am.LoadClassDatabaseFromPackage(fileInst.file.Metadata.UnityVersion);

var textureInf = fileInst.file.GetAssetInfo(12345);
var textureBase = am.GetBaseField(fileInst, textureInf);
var texture = TextureFile.ReadTextureFile(textureBase); // load base field into helper class
var textureBgraRaw = texture.GetTextureData(fileInst); // get the raw bgra32 data
var textureImage = Image.LoadPixelData<Bgra32>(textureBgraRaw, texture.m_Width, texture.m_Height); // use imagesharp to convert to image
textureImage.Mutate(i => i.Flip(FlipMode.Vertical)); // flip on x-axis (all textures in unity are stored flipped like this)
textureImage.SaveAsPng("texture.png");