There is an issue with modifying texture2d data in the bundle file using at2/at3
After modifying the texture2d data in the bundle file using at2/at3, there is a probability that this error will occur:
Mismatched serialization in the builtin class 'Texture2D'. (Read 264 bytes but expected 240 bytes)
I will experience stable issues when using AT2 and AT3 separately.
var assetsMananger = new AssetsManager();
assetsMananger.LoadClassPackage(newtpk);
var bunInst = assetsMananger.LoadBundleFile(ab);
int fileTotal = bunInst.file.GetAllFileNames().Count;
for (int i = 0; i< fileTotal; i++)
{
var inst = assetsMananger.LoadAssetsFileFromBundle(bunInst, i, false);
assetsMananger.LoadClassDatabaseFromPackage(inst.file.Metadata.UnityVersion);
foreach(var inf in inst.file.GetAssetsOfType(AssetClassID.Texture2D))
{
var baseField = assetsMananger.GetBaseField(inst, inf);
var width = baseField["m_Width"].AsInt;
var height = baseField["m_Height"].AsInt;
var tf = TextureFile.ReadTextureFile(baseField);
var bytes = ..... // my new datas
tf.SetTextureDataRaw(bytes, width, height);
tf.m_TextureFormat = (int)TextureFormat.ETC_RGB4;
tf.m_MipMap = false;
tf.m_MipCount = 1;
tf.WriteTo(baseField);
inf.SetNewData(baseField);
......
When I Console.WriteLine SetNewData method baseField.WriteToByteArray().Length is 240 (Unity Expected).
This problem does not necessarily occur, I have a recurring AB here.
My production environment:
- Unity Editor: 2021.2.18
- Platform: WebGL
I will upload the relevant ab to GitHub, https://github.com/liuxinyumocn/blog/tree/master/docs/assetsTools In this directory, atlas.bundle is the original bundle, and atlas2.bundle was modified by me. If you need the Unity project, I can provide you with a very small project to address the issue of replication.
Your texture data is only 85 bytes: Tsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactx.c3ede7cc. Are you sure bytes is set to the right thing?
This is a piece of text that I randomly wrote down, which also meets the expected content. It means that this can be any byte I want.
To be precise, Unity encountered an error reading the byte data of the AB without parsing the texture data content.
According to the metadata, the engine is 2021.2.5f1c302\n2. From my experience, \n2 games run on custom engines and most likely there are extra fields at the end. You may want to look into building a custom class database with the new fields (which you're on your own to figure out, I haven't played with WebGL games)