码迷,mamicode.com
首页 > 编程语言 > 详细

使用Unity3D顺序播放一组动画

时间:2018-05-05 13:22:08      阅读:468      评论:0      收藏:0      [点我收藏+]

标签:nim   ldl   run   add   image   idt   anim   http   tor   

思路是:动态创建一个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/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顺序播放一组动画

标签:nim   ldl   run   add   image   idt   anim   http   tor   

原文地址:https://www.cnblogs.com/yisawatbek/p/8994329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!