使用Unity3D順序播放一組動畫
阿新 • • 發佈:2018-05-05
mach eol 鏈接 iou unity3d sets tor star upd
思路是:動態創建一個AnimatorController,根據動畫的名字創建State,找到對應的AnimationClip,將它加入到State的Motion中,最後將State連起來。
貼代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEditorInternal; public class PlayerAnimatorController : MonoBehaviour { //from WPF privateList<string> NLPresult = new List<string>(); // Use this for initialization void Start () { //create animator controller UnityEditor.Animations.AnimatorController animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath("Assets/animationController.controller"); TestAddNLPresult (NLPresult); AddStateTransition (NLPresult, animatorController); GameObject TheOldLady = GameObject.Find ("The Old Lady"); Animator animator = TheOldLady.GetComponent<Animator> (); animator.runtimeAnimatorController = AssetDatabase.LoadAssetAtPath ("Assets/animationController.controller", typeof(UnityEditor.Animations.AnimatorController)) as UnityEditor.Animations.AnimatorController; } // Update is called once per frame void Update () { } //add animations from ListString private void AddStateTransition(List<string> animationNames,UnityEditor.Animations.AnimatorController animatorController) { //Get StateMachine var rootStateMachine = animatorController.layers [0].stateMachine; int i = 0; UnityEditor.Animations.AnimatorState state1 = new UnityEditor.Animations.AnimatorState(); UnityEditor.Animations.AnimatorState state2 = new UnityEditor.Animations.AnimatorState(); foreach(string name in animationNames) { //load animation Motion motion = AssetDatabase.LoadAssetAtPath ("Assets/StudioNewPunch/mayaAnimation/Boy@"+name+".fbx",typeof(Motion)) as Motion; //add States and Transations if (i % 2 == 0) { state1 = rootStateMachine.AddState(name); state1.motion = motion; state2.AddTransition (state1,true); } else { state2 = rootStateMachine.AddState(name); state2.motion = motion; state1.AddTransition (state2,true); //Debug.Log (state1); } i++; } } //test NLPlist private void TestAddNLPresult(List<string> TestList) { if (TestList != null) { TestList.Clear(); } TestList.Add ("2"); TestList.Add ("4"); TestList.Add ("5"); TestList.Add ("6"); TestList.Add ("7"); TestList.Add ("237"); TestList.Add ("238"); TestList.Add ("239"); TestList.Add ("8"); TestList.Add ("9"); TestList.Add ("10"); TestList.Add ("11"); TestList.Add ("242"); TestList.Add ("245"); TestList.Add ("300"); TestList.Add ("12"); TestList.Add ("13"); TestList.Add ("101"); TestList.Add ("102"); TestList.Add ("112"); TestList.Add ("113"); TestList.Add ("222"); TestList.Add ("224"); TestList.Add ("103"); TestList.Add ("104"); TestList.Add ("105"); TestList.Add ("106"); TestList.Add ("114"); TestList.Add ("179"); TestList.Add ("185"); TestList.Add ("107"); TestList.Add ("108"); TestList.Add ("109"); TestList.Add ("110"); TestList.Add ("111"); TestList.Add ("229"); TestList.Add ("241"); TestList.Add ("106"); } }
以上是我們項目裏用到的一些動畫,其中Boy為模型,其他的是導出的動畫,都是fbx格式的。
這是動態創建animatorController的結果,沒有鏈接Exit,等以後再鏈接。
效果嘛,特別棒,能夠順序播放任何一組動畫。
使用Unity3D順序播放一組動畫