Created
September 22, 2018 17:07
-
-
Save GhatSmith/f7bed47f55b327da432e32bfe87ea379 to your computer and use it in GitHub Desktop.
AnimatorControllerExtension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Reflection; | |
using System.Linq; | |
using UnityEngine; | |
using UnityEditor.Animations; | |
public static class AnimatorControllerExtension | |
{ | |
private static MethodInfo getEffectiveAnimatorControllerMethodInfo = null; | |
public static AnimatorController GetAnimatorController(Animator animator) | |
{ | |
if (getEffectiveAnimatorControllerMethodInfo == null) getEffectiveAnimatorControllerMethodInfo = typeof(AnimatorController).GetMethod("GetEffectiveAnimatorController", BindingFlags.Static | BindingFlags.NonPublic); | |
return getEffectiveAnimatorControllerMethodInfo.Invoke(null, new object[] { animator }) as AnimatorController; | |
} | |
public static ChildAnimatorState[] GetAllAnimatorStates(this AnimatorController animatorController) | |
{ | |
List<ChildAnimatorState> result = new List<ChildAnimatorState>(); | |
for (int i = 0; i < animatorController.layers.Length; i++) | |
{ | |
result.AddRange(animatorController.layers[i].stateMachine.GetAllAnimatorStates()); | |
} | |
return result.ToArray(); | |
} | |
// Recursive method to work with multiple layers of sub stache machines | |
private static ChildAnimatorState[] GetAllAnimatorStates(this AnimatorStateMachine animatorStateMachine) | |
{ | |
List<ChildAnimatorState> result = new List<ChildAnimatorState>(animatorStateMachine.states); | |
result.AddRange(animatorStateMachine.states); | |
for (int i = 0; i < animatorStateMachine.stateMachines.Length; i++) | |
{ | |
result.AddRange(animatorStateMachine.stateMachines[i].stateMachine.GetAllAnimatorStates()); | |
} | |
return result.ToArray(); | |
} | |
public static AnimatorStateTransition[] GetAllAnimatorTransitions(this AnimatorController animatorController) | |
{ | |
return GetAllAnimatorStates(animatorController).SelectMany(x => x.state.transitions).ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment