Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is an issue with modifying texture2d data in the bundle file using at2/at3 #118

Open
liuxinyumocn opened this issue Aug 17, 2023 · 5 comments

Comments

@liuxinyumocn
Copy link

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
@liuxinyumocn
Copy link
Author

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.

@nesrak1
Copy link
Owner

nesrak1 commented Aug 22, 2023

Your texture data is only 85 bytes: Tsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactxsactx.c3ede7cc. Are you sure bytes is set to the right thing?

@liuxinyumocn
Copy link
Author

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.

@liuxinyumocn
Copy link
Author

To be precise, Unity encountered an error reading the byte data of the AB without parsing the texture data content.

@nesrak1
Copy link
Owner

nesrak1 commented Aug 22, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants