how can I get the number of motions in a blend tree?

I found such an answer on this question:

	public static int GetNumberMotionsInBlendTree (Animator _Animator, int _iLayer, int _iHashBlendTree)
	{
		UnityEditorInternal.AnimatorController _AnimatorController = _Animator.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
		UnityEditorInternal.StateMachine _StateMachine = _AnimatorController.GetLayer (_iLayer).stateMachine;
		for (int i = 0; i < _StateMachine.stateCount; i++) 
		{
			UnityEditorInternal.State state = _StateMachine.GetState(i);
			if (state.uniqueNameHash == _iHashBlendTree) 
			{
				UnityEditorInternal.BlendTree _blendTree = state.GetMotion () as UnityEditorInternal.BlendTree;
				return _blendTree.childCount;
			}
		}		
		return 0;
	}

It allow me use random animation in my script regardless of the action of animators… :slight_smile: It is something like this:

    // My Event: should be in the end of each "hit"-animation clip
	void OnHitEnd ()
	{
        // I use the range [0..1000] for more randomizing
		int _iClipIndex = Random.Range (0, 1000) % m_iNumberAnimsInHits;

        // "m_iHitSelectID" is a parameter of my blend tree for "hit"-animation
		m_Animator.SetFloat (m_iHitSelectID, (float)_iClipIndex);
	}

And the last… I mean “how to use it”:

void Awake ()
{
     ...
     // get a hash-code for a parameter of blend tree (named "hitSelect")
     m_iHitSelectID = Animator.StringToHash ("hitSelect");    

     // get a hash-code for our blend tree (named "Hits") from base layer (named "Base")
     m_iHitStateID = Animator.StringToHash("Base.Hits");

     // get the number of motions in the blend tree
     m_iNumberAnimsInHits = ProjectTools.GetNumberMotionsInBlendTree (m_Animator, 0, m_iHitStateID);         
     ...
}