Search Unity

StateMachineBehaviours - Get from Editor Script

Discussion in 'Animation' started by Newcomma, Mar 19, 2015.

  1. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Hi There,

    I want to use an editor script to set a state machine behaviour property from the editor. My code looks something like:

    Code (CSharp):
    1. [UnityEditor.MenuItem("Set Sprites With Tag On Behaviour")]
    2. static void SetSpritesWithTagOnBehaviour ()
    3. {
    4.     Sprite[] mySprites = FindSpritesFromPackingTagInImporters();
    5.  
    6.     Animator animator = GameObject.Find ("MyStateMachine").GetComponent<Animator> ();
    7.     MyBehaviour behaviour = animator.GetBehaviour<MyBehaviour> ();
    8.  
    9.     behaviour.sprites = mySprites;
    10. }
    However from my static editor script function, behaviour is null, even though it exists on the animator.

    From the docs (https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours) I can see that GetBehaviour may only be valid from 'start'. However I need to be able to set my state machine behaviour properties offline.

    Of course I can do this directly through the UI (go to the animator, pick a state machine behaviour and change a public property) without running the game, so I assume I can also do this somehow through an editor script. Is this functionality possible? Or currently a limitation?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    There is two thing, the animator controller and the animator.

    The animator controller is an asset which contains statemachine, state and state machine behaviour. This is the asset that you build in the editor.

    The animator is the component that take care of evaluating the controller and manage all runtime instance.
    Animator.GetBehaviour<>() will return instance of SMB only when the game is running.


    So in your case you want to setup your controller, unfortunately there is no function like GetBehaviours<> that take a type as parameter and return all behaviour that match, you will need to check every controller's state and statemachine to find where your behaviour is stored.
    http://docs.unity3d.com/ScriptReference/Animations.AnimatorState-behaviours.html
    http://docs.unity3d.com/ScriptReference/Animations.AnimatorStateMachine-behaviours.html
    http://docs.unity3d.com/ScriptRefer...orController.GetStateEffectiveBehaviours.html

    I will add a new function AnimatorController.GetBehaviours<>, I think it make sense when you want to setup behaviour from script. unfortunately it won't be available until our next release.

    Best regards,
     
    theANMATOR2b likes this.
  3. Newcomma

    Newcomma

    Joined:
    Feb 10, 2015
    Posts:
    89
    Ah I see, thanks for the clarity! And thanks for adding the API for the next release, That will be ideal.

    Looking forward to 5.1 :)

    The code based on your explanation is the following (for anyone else curious):

    Code (CSharp):
    1.         Animator animator = GameObject.Find ("MyStateMAchine").GetComponent<Animator> ();
    2.         UnityEditor.Animations.AnimatorController controller = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
    3.  
    4.         foreach (AnimatorControllerLayer layer in controller.layers)
    5.         {
    6.             foreach (ChildAnimatorState childAnimState in layer.stateMachine.states)
    7.             {
    8.                 foreach(StateMachineBehaviour smb in childAnimState.state.behaviours)
    9.                 {
    10.                     MyBehaviour myBehaviour = smb as MyBehaviour;
    11.  
    12.                     if (myBehaviour != null)
    13.                     {
    14.                         if (myBehaviour.identiferString == myString)
    15.                         {
    16.                             myBehaviour.param = newvalue;
    17.                         }
    18.                     }
    19.                 }
    20.             }
    21.         }
     
    Last edited: Mar 19, 2015
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675