- Home /
How to create animation from FBX at runtime, not in editor!
Note: Everything creating at runtime.
1)loading fbx file from assert at runtime:
GameObject go = Instantiate(Resources.Load(path)) as GameObject;
2)then apply material:
Texture texture = Resources.Load (texturePath) as Texture
SkinnedMeshRenderer renderer = GetComponentInChildren();
renderer.material.shader = Shader.Find ("Diffuse");
renderer.material.mainTexture = texture;
3)create animation clip from "Take 001": But how can I get animation "Take 001" from the FBX.
gameObject.animation is null.
I know, there are one way to slove this pro: In editor inspector option, setting RIG to "legacy" mode, the Animation component will auto attaching, and "Take 001" will auto attaching under animations too.
But that's no what i need, i want create animation component at run time, but i can found a way to get animation clip from the FBX file, and attach it under animations.
Answer by Bunny83 · Apr 28, 2015 at 11:04 AM
By just reading your title the answer is very simple: You can't. Unity's FBX importer only exists in the editor. To make that clear: You can not import an FBX file at runtime.
However placing an FBX file in the resources folder of your project already imports the model in the editor. From now on the model is represented by Unity's internal model-asset format. When you use Resources.Load you simply grab the serialized model from the assets.
The imported model is actually split up into several sub assets (which you see when you expand the actual model file). The main asset is usually a GameObject which acts as root object. However you can simply use:
// C#
AnimationClip anim = Resources.Load<AnimationClip>("MyModel");
to get the animation clip asset which is simply a sub asset. Keep in mind that there's also the "LoadAll" method in case your model has several animations:
// C#
AnimationClip[] anims = Resources.LoadAll<AnimationClip>("MyModel");
foreach(var a in anims)
{
Debug.Log("Animation: " + a.name);
}
The solution is great, but what if you want to load only a specific animation, lets say "Idle2" and you can't afford to load all the animations and discard the ones you don't need?. With the default Resources.Load you load only the first clip
@Bravo_cr: I don't unterstand what you mean? You can't afford loading all the animations? The moment they are visible in the Hierarchy they all have been imported and stored as assets in your project. I just iterate over all animations that are stored as subassets in a certain model file.
If you worry about the build size you shouldn't place your models in a Resources folder. Things in Resources folders are always included in a build. That's why the usual way to work with assets is to reference the assets you need directly.
For example in a $$anonymous$$onoBehaviour script you can declare a public array like this:
public AnimationClip[] animationClips;
In the Unity editor you can now drag and drop those AnimationClips you need onto the array to store the reference. If you do that it's important to move your model out of a "Resources" folder. Now when you build your game only the animations which are referenced from your script are included in the build.
@Bunny83: Sorry I haven't explain me well. I was thinking of doing it at runtime. Lets say we have a FBX called "Spell Skills animations". In that FBX there are 20 animations (TPose, Spell A, Spell B, Spell C...). Now my in game characters can only have one spell equipped at a time, so as an example lets say my main character needs to load "Spell C" only. Imagine that we can't afford to load all the animations of the FBX and then discard the ones that are not needed because of memory constrains. Do you know if its possible?
@Bravo_cr: As i said if you have your animations in the resources folder they are all loaded into memory when your game starts. All assets in the resources folder are loaded to be available when you use Resources.Load. So it doesn't make any difference. Also animations are one of the smallest assets possible ^^. A single texture is usually way larger than all your animations together.
And to answer your question: No, sub assets can't be accessed by "name". The only way is iterating through them.
Another possibility is that you store your animations as seperate animation assets. For that you have to write an editor script that creates seperate assets files for each animation. We once did this for our student project. The only reason was to get red of the model file. We had each animation as a seperate FBX file. That wasn't a problem since when you create a build only the animation is included in the build. However we wanted to reduce the size of our project folder. We has around 80 animations where each FBX was about 4-5 $$anonymous$$B. Once we "extracted" the animations from the FBX each was about 70$$anonymous$$B i think ^^.
Here's a link to my EditorWindow which allows you to extract AnimationClips from an FBX file as standalone asset. However keep in $$anonymous$$d that you can't edit or simply reimport the original asset to update those. You have to reimport / update the original FBX and extract them again.
This is an editor script so place it in a folder called "editor", otherwise it will not work. You can open the editor window by using Unity's main menu. There will be a new item called "Tools"
@Bravo_cr: That strongly depends on to which platform you build. Usually you're right that assets in a resources folder are loaded when they are first requested (and will stay loaded from that point on). In a webbuild for example everything is actually in memory as the whole project is unpacked in memory. However memory management is a completely different topic ^^.
btw: If you just want to have a global way to access animations, you can simply create a prefab with a special script on it that just holds an array with AnimationClips. You don't even need to instantiate that prefab in order to access the array:
public class AnimationCollection : $$anonymous$$onoBehaviour
{
private static AnimationCollection m_Instance = null;
public static AnimationCollection Instance
{
get {
if (m_Instance == null)
m_Instance = (AnimationCollection)FindObjectOfType(typeof(AnimationCollection));
return m_Instance;
}
}
public AnimationClip[] animations;
}
Just attach this script to an empty Gameobject and turn it into a prefab. Now you can edit the prefab in the project view and drag & drop all animations you have to that array. If you need one of those animations you can simply use
AnimationCollection.Instance.animations[5]
to get the 6th animation in the array. This would work from any script.
Your answer
Follow this Question
Related Questions
Possibility of adding animation files during runtime? 1 Answer
How to export FBX at runtime with animation(s), rig and the mesh to Maya, blender etc.. ? 1 Answer
Manually Animating Bones then blending with existing animations or saving importing as fbx 3 Answers
Character Change its Position When Animation Apply on It 0 Answers
Mecanim animation clip mask 1 Answer