unity5.x代码创建AnimatorController状态机

前言

当模型太多时。如果需要一个一个的创建AnimatorController其实很麻烦的。一个一个将动作拖入。创建AnimatorController什么的。很是烦躁。之前有人分享了5.x以前的代码创建方法。

但是在5.0之后。并不好用了。unity改变了原来的方式。所以就分享下。给需要的人。

代码

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditor.Animations;//5.0改变 UnityEditorInternal;并不能用了。

public class CreateAnimatorController : Editor 
{
    [MenuItem("ModelConfig/创建Controller")]
    static void DoCreateAnimationAssets()
    {
        //创建Controller
        AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath("Assets/animation.controller");
        //得到它的Layer
        AnimatorControllerLayer layer = animatorController.layers[0];
        //将动画保存到 AnimatorController中
        AddStateTransition("Assets/Art Resources/Character/moster-002/basic/moster-002@run.FBX", layer);
        AddStateTransition("Assets/Art Resources/Character/moster-002/basic/moster-002@stand.FBX", layer);
        AddStateTransition("Assets/Art Resources/Character/moster-002/basic/moster-002@born.FBX", layer);
    }

    private static void AddStateTransition(string path, AnimatorControllerLayer layer)
    {
        AnimatorStateMachine sm = layer.stateMachine;
        //根据动画文件读取它的AnimationClip对象
        AnimationClip newClip = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip;
        
        ////取出动画名子 添加到state里面
        AnimatorState state = sm.AddState(newClip.name);
        //5.0改变
        state.motion = newClip;
        Debug.Log(state.motion);
        //把state添加在layer里面
        AnimatorStateTransition trans = sm.AddAnyStateTransition(state);
    }
}

 注意事项

state.motion这样直接赋值的话。需要注意下动作fbx的设置。需要是如下的设置AnimationType设置为Generic通用类型。如下

1

 

效果图

2

 

 

 

 

 

本文链接:

https://bobsong.cn/973.html
1 + 8 =
9 评论
    2015年06月15日 回复

    好东西!支持!实在不知道怎样才能更好地感激,期待有更多的分享!谢谢!

      2015年06月15日 回复
        2015年06月15日 回复

        @小宝 小宝93年的?大学在哪读的呢,这么厉害

          2015年06月16日 回复

          @郝伟 江西。南昌

    2016年03月31日 回复

    我用的这个方法创建的 结果本机是有连线的 提交到svn后其它人更新的全是没有连线的

      2016年04月05日 回复

      @刘远 你少提交了东西呗

    2016年03月31日 回复

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using UnityEditorInternal;
    using System;
    using System.IO;
    using System.Collections.Generic;
    public class AutoMakeAnimatorController : Editor
    {

    
    private static string HPath = "Assets/ArtResources/Original/CommonAnimation";
    
    [MenuItem("Game/自动生成AnimatorController")]
    static void DoCreateAnimationAssets()
    {
        List states = new List();
        List actions = new List();
        actions.Add(AnimationType.Idle);
        actions.Add(AnimationType.Walk);
        actions.Add(AnimationType.L_Walk);
        actions.Add(AnimationType.R_Walk);
        actions.Add(AnimationType.B_Walk);
        actions.Add(AnimationType.Run);
        actions.Add(AnimationType.Hit);
        actions.Add(AnimationType.Fly_1);
        actions.Add(AnimationType.Fly_2);
        actions.Add(AnimationType.Fly_3);
        actions.Add(AnimationType.Die);
        actions.Add(AnimationType.Win);
        actions.Add(AnimationType.Att_1);
        actions.Add(AnimationType.Att_2);
        actions.Add(AnimationType.Att_3);
        actions.Add(AnimationType.Skill);
        actions.Add(AnimationType.Block);
        actions.Add(AnimationType.Blockback);
        actions.Add(AnimationType.Dfwalk);
        actions.Add(AnimationType.In);
        UnityEngine.Object select = Selection.activeObject;
        string p = AssetDatabase.GetAssetPath(select);
        if (!Directory.Exists(p))
        {
            return;
        }
        if (p == HPath)
        {
            return;
        }
        if (!p.Contains(HPath))
        {
            return;
        }
    
        string dPath = p;// "Assets/animations/Ss";
    
        string name = Path.GetFileName(dPath);
        string fp = "Assets/ArtResources/Original/CommonAnimation/Animator/" + name + ".controller";
        if (File.Exists(fp))
        {
            return;
        }
        //创建animationController文件,保存在Assets路径下
        UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(fp);
        //得到它的Layer, 默认layer为base 你可以去拓展
        UnityEditor.Animations.AnimatorControllerLayer layer = animatorController.layers[0];
    
        string[] files = Directory.GetFiles(dPath);
        states.Clear();
        for(int i = 0; i < actions.Count; i++)
        {
            string action = actions[i];
            string file = dPath + "/" + action + ".FBX";
            UnityEditor.Animations.AnimatorState state = AddStateTransition(animatorController, file, layer);
            states.Add(state);
        }
    
        //foreach (string file in files)
        //{
        //    if (file.EndsWith(".FBX"))
        //    {
        //        UnityEditor.Animations.AnimatorState state = AddStateTransition(animatorController, file, layer);
        //        states.Add(state);
        //    }
        //}
    
        foreach (UnityEditor.Animations.AnimatorState fState in states)
        {
            if (fState.name == "Idle")
            {
                //layer.stateMachine.AddEntryTransition(fState);
                layer.stateMachine.defaultState = fState;
            }
            foreach (UnityEditor.Animations.AnimatorState tState in states)
            {
                if (fState != tState)
                {
                    UnityEditor.Animations.AnimatorStateTransition at = new UnityEditor.Animations.AnimatorStateTransition();
                    at.destinationState = tState;
                    at.hasExitTime = false;
                    at.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 1f, tState.name);
                    fState.AddTransition(at);
                    //fState.AddTransition(tState);
                }
            }
        }
    
    }
    
    
    
    private static UnityEditor.Animations.AnimatorState AddStateTransition(UnityEditor.Animations.AnimatorController animatorController, string path, UnityEditor.Animations.AnimatorControllerLayer layer)
    {
        ////根据动画文件读取它的AnimationClip对象
        AnimationClip newClip = null;
        if (File.Exists(path))
        {
            newClip = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip;
        }
        string[] sL = Path.GetFileName(path).Split(".".ToCharArray());
        string aName = sL[0];
    
        UnityEngine.AnimatorControllerParameter p = new UnityEngine.AnimatorControllerParameter();
        p.type = UnityEngine.AnimatorControllerParameterType.Bool;
        p.name = aName;
        p.defaultBool = false;
        animatorController.AddParameter(p);
    
        UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;
    
        ////取出动画名子 添加到state里面
        UnityEditor.Animations.AnimatorState state = sm.AddState(aName);
        if(newClip != null)
            state.motion = newClip;
        
        return state;
    
        //把state添加在layer里面
        //UnityEditor.Animations.AnimatorStateTransition ts = sm.AddAnyStateTransition(state);
    }

    }

    2016年03月31日 回复

    见我贴的代码

    2016年03月31日 回复

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using UnityEditorInternal;
    using System;
    using System.IO;
    using System.Collections.Generic;
    public class AutoMakeAnimatorController : Editor
    {

    
    private static string HPath = "Assets/ArtResources/Original/CommonAnimation";
    
    [MenuItem("Game/自动生成AnimatorController")]
    static void DoCreateAnimationAssets()
    {
        List states = new List();
        List actions = new List();
        actions.Add(AnimationType.Idle);
        actions.Add(AnimationType.Hit);
        actions.Add(AnimationType.Walk);
        actions.Add(AnimationType.L_Walk);
        actions.Add(AnimationType.R_Walk);
        actions.Add(AnimationType.B_Walk);
        actions.Add(AnimationType.Run);
    
        actions.Add(AnimationType.Fly_1);
        actions.Add(AnimationType.Fly_2);
        actions.Add(AnimationType.Fly_3);
        actions.Add(AnimationType.Die);
        actions.Add(AnimationType.Win);
        actions.Add(AnimationType.Att_1);
        actions.Add(AnimationType.Att_2);
        actions.Add(AnimationType.Att_3);
        actions.Add(AnimationType.Skill);
        actions.Add(AnimationType.Block);
        actions.Add(AnimationType.Blockback);
        actions.Add(AnimationType.Dfwalk);
        actions.Add(AnimationType.In);
        UnityEngine.Object select = Selection.activeObject;
        string p = AssetDatabase.GetAssetPath(select);
        if (!Directory.Exists(p))
        {
            return;
        }
        if (p == HPath)
        {
            return;
        }
        if (!p.Contains(HPath))
        {
            return;
        }
    
        string dPath = p;// "Assets/animations/Ss";
    
        string name = Path.GetFileName(dPath);
        string fp = "Assets/ArtResources/Original/CommonAnimation/Animator/" + name + ".controller";
        if (File.Exists(fp))
        {
            return;
        }
        //创建animationController文件,保存在Assets路径下
        UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(fp);
        //得到它的Layer, 默认layer为base 你可以去拓展
        UnityEditor.Animations.AnimatorControllerLayer layer = animatorController.layers[0];
    
        string[] files = Directory.GetFiles(dPath);
        states.Clear();
        for(int i = 0; i < actions.Count; i++)
        {
            string action = actions[i];
            string file = dPath + "/" + action + ".FBX";
            UnityEditor.Animations.AnimatorState state = AddStateTransition(animatorController, file, layer);
            states.Add(state);
        }
    
        //foreach (string file in files)
        //{
        //    if (file.EndsWith(".FBX"))
        //    {
        //        UnityEditor.Animations.AnimatorState state = AddStateTransition(animatorController, file, layer);
        //        states.Add(state);
        //    }
        //}
    
        foreach (UnityEditor.Animations.AnimatorState fState in states)
        {
            if (fState.name == "Idle")
            {
                //layer.stateMachine.AddEntryTransition(fState);
                layer.stateMachine.defaultState = fState;
            }
            foreach (UnityEditor.Animations.AnimatorState tState in states)
            {
                if (fState != tState)
                {
                    UnityEditor.Animations.AnimatorStateTransition at = new UnityEditor.Animations.AnimatorStateTransition();
                    at.destinationState = tState;
                    at.hasExitTime = false;
                    at.AddCondition(UnityEditor.Animations.AnimatorConditionMode.If, 1f, tState.name);
                    fState.AddTransition(at);
                    //fState.AddTransition(tState);
                }
            }
        }
    
    }
    
    
    
    private static UnityEditor.Animations.AnimatorState AddStateTransition(UnityEditor.Animations.AnimatorController animatorController, string path, UnityEditor.Animations.AnimatorControllerLayer layer)
    {
        ////根据动画文件读取它的AnimationClip对象
        AnimationClip newClip = null;
        if (File.Exists(path))
        {
            newClip = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip;
        }
        string[] sL = Path.GetFileName(path).Split(".".ToCharArray());
        string aName = sL[0];
    
        UnityEngine.AnimatorControllerParameter p = new UnityEngine.AnimatorControllerParameter();
        p.type = UnityEngine.AnimatorControllerParameterType.Bool;
        p.name = aName;
        p.defaultBool = false;
        animatorController.AddParameter(p);
    
        UnityEditor.Animations.AnimatorStateMachine sm = layer.stateMachine;
    
        ////取出动画名子 添加到state里面
        UnityEditor.Animations.AnimatorState state = sm.AddState(aName);
        if(newClip != null)
            state.motion = newClip;
        
        return state;
    
        //把state添加在layer里面
        //UnityEditor.Animations.AnimatorStateTransition ts = sm.AddAnyStateTransition(state);
    }

    }