Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create .anim file from .fbx file in Unity?

Tags:

unity3d

fbx

I am new in using Unity. In my game, models and animations are exported from 3DMAX as .fbx files, animations are clipped in Unity, but there's no .anim files generated, I need separated .anim files because the following code will not work even I have clipped animation "run":

var clip = animation["run"];

Can someone help me? thanks in advance.

rigmodelanimationsimport settings

like image 301
Cosmore Avatar asked Mar 26 '14 13:03

Cosmore


2 Answers

I finally found the answer, pressing CTRL+D on animation clip in .fbx file will create a separate .anim file, what a strange operation!

like image 169
Cosmore Avatar answered Sep 22 '22 03:09

Cosmore


I wrote a script to do this easily

enter image description here

using System.IO;
using UnityEditor;
using UnityEngine;

public class AnimationExtractor: MonoBehaviour
{
    [MenuItem("Assets/Extract Animation")]
    private static void ExtractAnimation()
    {
        foreach (var obj in Selection.objects)
        {
            var fbx = AssetDatabase.GetAssetPath(obj);
            var directory = Path.GetDirectoryName(fbx);
            CreateAnim(fbx, directory);     
        }
    }

    static void CreateAnim(string fbx, string target)
    {
        var fileName = Path.GetFileNameWithoutExtension(fbx);
        var filePath = $"{target}/{fileName}.anim";
        AnimationClip src = AssetDatabase.LoadAssetAtPath<AnimationClip>(fbx);
        AnimationClip temp = new AnimationClip();
        EditorUtility.CopySerialized(src, temp);
        AssetDatabase.CreateAsset(temp, filePath);
        AssetDatabase.SaveAssets();
    }
}
like image 21
Seyed Morteza Kamali Avatar answered Sep 22 '22 03:09

Seyed Morteza Kamali