Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reconstruct AnimationClip from bundle binary asset bytes? (Bundling problem)

Discussion in 'Scripting' started by imerso, Aug 30, 2017.

  1. imerso

    imerso

    Joined:
    May 6, 2011
    Posts:
    278
    I am developing a solution to bundle many items separately, and everything *except* AnimationClip works. I can bundle rigs, skins, textures, meshes, etc all separately in different bundles, and reconstruct them from their bundles after downloading them from the server.

    The AnimationClips won't work, because for some reason Unity does not bundle them correctly. They simply bundle as empty animations. That seems to be an old bug which was never fixed.

    As an alternative, I am now bundling the AnimationClips as binary assets -- according to https://docs.unity3d.com/550/Documentation/Manual/binarydata.html

    Now, that sort of works, but Unity seems to lack a final step: load the AnimationClip back from the binary TextAsset... I can bundle the animations as binary TextAssets inside their individual bundles, and I confirmed that the binary asset indeed contains the AnimationClip bytes, because if I load the TextAsset from the bundle, and File.WriteAllBytes() those bytes to a .asset, I can later on load the AnimationClip using Resources.Load() -- not in the same running session, though. But that is obviously not a complete solution, that is an ugly hack, and I need to reconstruct the AnimationClip from the TextAsset bytes, directly from memory (from the TextAsset.bytes). Is that possible at all?

    If there is no way around saving the bytes to the .asset before loading, then is that at least possible to save to persistentDataPath or somewhere else that will be supported in all platforms, and how to load AnimationClip from there then?

    Also, taking the opportunity, why there is no way to delete an individual bundle from the Caching?

    To Unity people: please try to give an answer to this, I've found many other threads with people mentioning that AnimationClip does *not* bundle correctly, and all them without a response.

    Note: For this project I am stuck with 5.2.2, but I also tested on 5.6.1 and it has the same AnimationClip bundling bug...

    Thanks.
     
    Last edited: Aug 30, 2017
  2. imerso

    imerso

    Joined:
    May 6, 2011
    Posts:
    278
    Ok, so after some struggling, I fixed it. To help someone else who may come looking for a workaround, here it is:

    The problem: If you try to separate everything (rigs, skins, meshes, textures, animations, etc) into many independent downloadable bundles, so to have really small and customized dlc's, even if you extract the AnimationClips to .asset or .anim files, they won't get bundled correctly.

    The solution: It will bundle the AnimationClips correctly if you set the "legacy" flag before including them into their bundles, though. And that was what I did to solve the problem. On the game side, when reloading the bundle, I stopped using the new Animator and used the old Animation component instead. Of course that has a few implications but for this particular project, that was not a problem at all.

    Regarding the bundles caching problem, I ended up ditching the incomplete built-in caching system and implementing my own caching with all the needed features. The internal caching that Unity provides is incomplete and can't be used for anything above trivial, basic usage. Be warned, before you lose many days on that.

    Another problem I had (not mentioned here) was with the skinned meshes separated from their skeletons. They would lose the bones links. I solved that by saving the bones info into a text file and bundling the text file together with the skins. Then on load-time I rebuilt the bones information by parsing that text-file and finally re-linking to the proper bones on the skeleton.

    Please note that I really like Unity overall, but it's definitely not perfect. At least not yet. =)