- Home /
How to load a file after build
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!
Answer by CHPedersen · Jan 19, 2015 at 07:55 AM
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.
Im not sure I understand the answer. I've never setup asset bundles - can I build an asset bundle on the fly from code to allow me to import an FBX of my choosing?
I have added more information to my answer now, please review and see if that helps. :)
https://docs.unity3d.com/$$anonymous$$anual/AssetBundles-Building.html is the current version of the URL you mention
Answer by DaDonik · Jan 19, 2015 at 07:57 AM
It's not for .fbx, but it circumvents the AssetBundles.
Except that the obj format doesn't support animations...
Your answer
Follow this Question
Related Questions
Mesh UV coords give weird result when FBX is imported at runtime 2 Answers
What is the best way to modify the Car Tutorial for more cars? 2 Answers
FBX Importer at runtime 2 Answers
How to add gameobject by fbx file at runtime ? 1 Answer
Manually Animating Bones then blending with existing animations or saving importing as fbx 3 Answers