Unity サブステートマシン内のステートをスクリプトから操作する


やりたいこと

AnimatorControllerのBase Layerに作ったAttackサブステートマシン内の全てのステートのmirrorをスクリプトから操作したい

スクリプト

using System.Collections;
using System.Collections.Generic;
using UnityEditor.Animations;
using UnityEngine;

private List<AnimatorState> attackStates = new List<AnimatorState>();

void Start()
{
    Animator _animator = GetComponent<Animator>();
    AnimatorController _animCon = _animator.runtimeAnimatorController as AnimatorController;
    AnimatorControllerLayer[] layers = _animCon.layers;
    foreach (AnimatorControllerLayer layer in layers)
    {
        if (layer.stateMachine.name == "Base Layer") //layer指定
        {
            ChildAnimatorStateMachine[] _animStateMachines = layer.stateMachine.stateMachines;
            foreach (ChildAnimatorStateMachine statemachine in _animStateMachines)
            {
                if (statemachine.stateMachine.name == "Attack") //sub-state machine指定
                {
                    foreach (ChildAnimatorState state in statemachine.stateMachine.states)
                    {
                        attackStates.Add(state.state);
                    }
                }
            }
        }
    }
}
public void switchMirror()
{
    foreach (AnimatorState state in attackStates)
    {
        state.mirror = !state.mirror; //mirrorを操作
        _animator.Rebind(); //リバインド忘れずに
    }
}

参考リンク

https://gametukurikata.com/animationanimator/statefootik
https://docs.unity3d.com/ja/current/ScriptReference/Animations.AnimatorStateMachine.html