[返回首页 ] 版权声明
加入收藏 网站地图
关于我们 联系我们
   最新文章
   推荐文章
   点击排行
Unity3D笔记
Unity3D生成模型动画控制器AnimatorContrer
来源:霜叶工作站 点击次数: 发布时间:2016-05-21 15:43:09

Editor模式下,批量生成模型的动画控制器Prefab对象:

using System.Collections;
using UnityEditor;
using UnityEditorInternal;
 
public class Test :Editor {
 [MenuItem("Test/Test")]
 static void DoCreateAnimationAssets()
 {
  //创建animationController文件,保存在Assets路径下
  AnimatorController animatorController = AnimatorController.CreateAnimatorControllerAtPath("Assets/animation.controller");
  //得到它的Layer, 默认layer为base 你可以去拓展
  AnimatorControllerLayer layer = animatorController.GetLayer(0);
  //把动画文件保存在我们创建的AnimationController中
  AddStateTransition("
Assets/Resources/airenlieren@Idle.FBX",layer);
  AddStateTransition("
Assets/Resources/attack@attack.FBX",layer);
  AddStateTransition("
Assets/Resources/aersasi@Run.FBX",layer);
 }
 
 private static void AddStateTransition(string path, AnimatorControllerLayer layer)
 {
  UnityEditorInternal.StateMachine sm = layer.stateMachine;
  //根据动画文件读取它的AnimationClip对象
  AnimationClip newClip = AssetDatabase.LoadAssetAtPath(path, typeof(AnimationClip)) as AnimationClip;
  //取出动画名子 添加到state里面
  State  state = sm.AddState(newClip.name);
  state.SetAnimationClip(newClip,layer);
  //把state添加在layer里面
  Transition trans = sm.AddAnyStateTransition(state);
  //把默认的时间条件删除
                trans.RemoveCondition(0);
 }
}

 

//5.0版本用的

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);
    }
}