Hi,
I am building a new game where I have an fbx file of animation, associated with an animation controller. This moves a model in the game. No big deal there.
In the file system, if I replace the fbx file with a different animation (actually overwrite the fbx file with a file of the same name) and run the game, the new animation is picked up. This works through the Unity IDE.
However, when I build the game and take the same steps, only the animation that existed while the game was being built will ever play in the game.
Is there a way to dynamically load new animations into the compiled unity game?
Thanks in advance for any pointers!
Models, including their animations, are built into resource files when you build your stand-alone player. They are not actually loaded from separate files once the game is finally built.
To load files outside the normal Resources.Load pipeline post-build, you have to use AssetBundles. There is currently no other way to do so, short of writing your own content importer.
EDIT: Added more info on Asset bundles and how to load them.
AssetBundles are not created at runtime, they are created in the editor with an editor script. One would normally have to write this script oneself, but Unity made a sample editor script we can copy paste into our projects and use. It’s available on this site, where you can also read in the docs about building AssetBundles:
http://docs.unity3d.com/Manual/BuildingAssetBundles.html
Copy paste that script into your project, then you can export your assets by rightclicking them and choosing Build AssetBundle From Selection. I only use the Track Dependencies version of it.
Afterwards, you now have an AssetBundle. This is a file that lives outside the normal Resources loading pipeline in Unity and can be loaded post build, which is what you require here. Loading them looks a little different from calling Resources.Load, but it’s almost as simple. It can be done with few lines of code, I do it like this:
AssetBundle assets = AssetBundle.CreateFromFile(Directory.GetCurrentDirectory() + "\\AssetBundles\\" + section.PartNumber + ".unity3d");
assets.LoadAll();
loadedObject = (GameObject)assets.mainAsset;
You have to supply your own path to AssetBundle.CreateFromFile, which is whereever you choose to store the .unity3d-file you originally exported using the editor script.
Hope that clears it up.
ObjImporter
It’s not for .fbx, but it circumvents the AssetBundles.