使用Unity3D顺序播放一组动画

Posted yisawatbek

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Unity3D顺序播放一组动画相关的知识,希望对你有一定的参考价值。

思路是:动态创建一个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
    private List<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/[email protected]"+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顺序播放一组动画的主要内容,如果未能解决你的问题,请参考以下文章

动画未按照编写代码的顺序运行

unity3d的Animation 动画播放器的基本API

依次播放多个音频片段

[Unity3D]Unity4新的动画系统Mecanim

Unity3D小技巧Unity3D中判断Animation以及Animator动画播放结束,以及动画播放结束之后执行函数

Unity3D日常开发Unity3D中Animation和Animator动画的播放暂停倒放控制