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(); //リバインド忘れずに
}
}
参考リンク
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
Author And Source
この問題について(Unity サブステートマシン内のステートをスクリプトから操作する), 我々は、より多くの情報をここで見つけました https://qiita.com/vodef6/items/913233790fbe86538910著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .