As the title states, I have a PropertyDrawer for a class that inherits from StateMachineBehaviour and I am trying to get the clip from the state the StateMachineBehaviour is attached to, but I have no idea whatsoever how to proceed with this.
Right now I have a public AnimationClip variable in my StateMachineBehaviour, but I would like to get rid of it since it seems redundant, every time I add the behaviour to a State, I have to set the same animation the State has into the behaviour variable.
My current goal is to get the number of frames and/or lenght of the clip in the PropertyDrawer so I can show it in the inspector of the behaviour.
It’s rather hard, because StateMachineBehaviour doesn’t contain any information to the state (public or private), but you can do this in editor:
AssetDatabase.GetAssetPath() of your StateMachineBehaviour, to get the AnimatorController asset path (every StateMachineBehaviour is a sub-asset of AnimatorController)
AssetDatabase.LoadAllAssetsAtPath() and find all the AnimationStates (again, states are sub-assets of AnimatorController)
access StateMachineBehaviour attached to the state by AnimationState.behaviours, and check if any is same as yours, then get the clip by AnimatorState.motion as AnimationCLip
A super fast example:
[CustomEditor(typeof(MyStateMachineBehaviour))]
public class MyStateMachineBehaviourEditor : Editor
{
private AnimatorState parentState;
private void OnEnable()
{
var path = AssetDatabase.GetAssetPath(target);
var states = AssetDatabase.LoadAllAssetsAtPath(path).OfType<AnimatorState>();
foreach (var state in states) {
if (state.behaviours.All(b => b != target)) continue;
parentState = state;
break;
}
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (parentState == null) return;
EditorGUILayout.LabelField("clip length:", (parentState.motion as AnimationClip).length.ToString("F2"));
}
}