Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by windflower · Apr 28, 2015 at 10:25 AM · animationfbxruntimeanimation clip

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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);
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bravo_cr · May 05, 2015 at 01:55 PM 0
Share

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

avatar image Bunny83 · May 05, 2015 at 06:02 PM 1
Share

@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.

avatar image Bravo_cr · May 06, 2015 at 05:52 AM 0
Share

@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?

avatar image Bunny83 · May 06, 2015 at 07:27 PM 0
Share

@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"

avatar image Bunny83 · May 07, 2015 at 02:57 PM 1
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges