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通用类型。如下
效果图
好东西!支持!实在不知道怎样才能更好地感激,期待有更多的分享!谢谢!
@xxoo
@小宝 小宝93年的?大学在哪读的呢,这么厉害
@郝伟 江西。南昌
我用的这个方法创建的 结果本机是有连线的 提交到svn后其它人更新的全是没有连线的
@刘远 你少提交了东西呗
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.IO;
using System.Collections.Generic;
public class AutoMakeAnimatorController : Editor
{
}
见我贴的代码
using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.IO;
using System.Collections.Generic;
public class AutoMakeAnimatorController : Editor
{
}