首页 > 解决方案 > 如何从子状态机获取动画?

问题描述

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public static class IListExtensions
{
    /// <summary>
    /// Shuffles the element order of the specified list.
    /// </summary>
    public static void Shuffle<T>(this IList<T> ts)
    {
        var count = ts.Count;
        var last = count - 1;
        for (var i = 0; i < last; ++i)
        {
            var r = UnityEngine.Random.Range(i, count);
            var tmp = ts[i];
            ts[i] = ts[r];
            ts[r] = tmp;
        }
    }
}

public class PlayAnimations : MonoBehaviour
{
    private AnimationClip[] clips;
    private Animator animator;

    private void Awake()
    {
        // Get the animator component
        animator = GetComponent<Animator>();

        // Get all available clips
        clips = animator.runtimeAnimatorController.animationClips;
    }

    private void Start()
    {
        StartCoroutine(PlayRandomly());
    }

    private IEnumerator PlayRandomly()
    {
        var clipList = clips.ToList();

        while (true)
        {
            clipList.Shuffle();

            foreach (var randClip in clipList)
            {
                animator.Play(randClip.name);
                yield return new WaitForSeconds(randClip.length);
            }
        }
    }
}

这将从常规顶部控制器状态机中的状态获取所有动画。但是我创建了一个子状态机名称 Magic StateMachine。

而且我只想在 Magic StateMachine 中从状态中获取所有动画剪辑。

动画控制器的屏幕截图:

动画控制器

以及子状态机的屏幕截图以及我想要从中获取动画片段并随机播放的状态:

子状态机

标签: c#unity3d

解决方案


推荐阅读