Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to Add StateMachineBehaviour at Runtime?

Discussion in 'Animation' started by kylin9872, Jan 24, 2015.

  1. kylin9872

    kylin9872

    Joined:
    Nov 3, 2014
    Posts:
    4
    Can I add or remove StateMachineBehaviour for an AnimatorState in Unity 5.0?
    Who can give some example or hint?
    Thank you?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    You cannot add or remove SMB at runtime, but you can activate/deactivate them
     
  3. kylin9872

    kylin9872

    Joined:
    Nov 3, 2014
    Posts:
    4
    Thank you!
     
  4. Anderson-Cardoso859

    Anderson-Cardoso859

    Joined:
    Mar 23, 2014
    Posts:
    61
    Hi Mecanim.Dev,

    How can I activate/deactivate a SMB at runtime? I mean by code, is it possible?
     
    Last edited: Mar 29, 2015
  5. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Damn you are right, there is no API function to activate/deactivate a SMB, the engine does support it as we check for SMB activeness before calling any callback. You can disable them via the inspector for now. We will add proper support for runtime ASAP.

    smbactive.png
     
  6. Anderson-Cardoso859

    Anderson-Cardoso859

    Joined:
    Mar 23, 2014
    Posts:
    61
    Thanks for the reply.

    From what I've tested the OnEnable/OnDisable callbacks are being called when I activate/deactivate the SMB in the Inspector, what is weird (for me at least) is that they are being called when I'm not in playmode.

    It would be awesome if the OnEnable/OnDisable callbacks were called during playmode only. And I could use the ExecuteInEditMode attribute if I also want these callbacks during editor (like in MonoBehaviours). Just a little suggestion.

    Keep the good work.

    Regards,
     
    Last edited: Mar 30, 2015
  7. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675

    Yes that make sense, I'm adding a new feature request for this
     
    Anderson-Cardoso859 likes this.
  8. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi Anderson,

    Just to let you know that OnEnable/OnDisable should now be called only in playmode, this new feature is making is way to 5.2

    For ExecuteInEditMode attribut we won't support it for state machine behaviour because they are invoked by the animator depending on which state is currently playing, In the editor the animator is not ticked so that wouldn't make any sense.
     
  9. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    got trouble OnStateEnter is called 2 time
    and change variable not working in runtime anymore in 5.3 ?
    any info about that's please
     
  10. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    @Mecanim-Dev, it seems it's still not possible to add and enable/disable StateMachineBehaviours at runtime, is that right or am I missing the way to do it?
     
  11. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    no there is no way to add a SMB at runtime. You can enable/disable SMB in the editor but at runtime it not possible at the moment.
    Enabling/Disabling at runtime is something I did try to implement in 5.0 but due to technical issue it was not possible.
    I did try last week again in 2017.1 and we still have the same technical issue.
    The problem is that SMB derived from scriptableobject which doesn't support enable/disable state and changing this cause a lot of regressions in other part of unity, the scripting team is aware of this limitation but they don't have the bandwidth to fix this.
     
  12. panta

    panta

    Joined:
    Aug 10, 2012
    Posts:
    71
    @Mecanim-Dev any news on this? It would be super useful to have...
     
  13. Mecanim-Dev

    Mecanim-Dev

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    @panta unfortunately no, this is not a high priority feature for scripting team
     
  14. panta

    panta

    Joined:
    Aug 10, 2012
    Posts:
    71
    @Mecanim-Dev thank you for the response. I ended up making a base class with an "enabled" field, and checking that before running any of the state machine behaviour functions. Pretty hacky/error-prone, but it's something I suppose...

    Hopefully the priority will be reconsidered in the future
     
  15. remiC3D

    remiC3D

    Joined:
    Dec 15, 2017
    Posts:
    30
    Hello, I've read this thread, so, I understand you can't add/remove a statemachine behavior at runtime, nor enable/disable it, is that correct ? If so, what would be the recommended way, from a mono behavior script, to ask an animator to play a particular state, THEN, to call (or not) method Foo() when this state exits ?
     
  16. EthanF4D

    EthanF4D

    Joined:
    Jan 9, 2018
    Posts:
    13
    Runtime add animation event on the clip of that state, that signal to your script Foo() is needed to be called, and remove the event afterwards.
    Obviously has its limitations
     
  17. EthanF4D

    EthanF4D

    Joined:
    Jan 9, 2018
    Posts:
    13
    On the other hand I have a half runtime solution. (not at runtime) add common state machine behaviours to relevant states that propagates whatever state machine event are received. E.g OnEnterStateBehaviour OnExitStateBehaviour.
    Then your custom script subscribe to those events.
     
  18. Xero82

    Xero82

    Joined:
    Mar 7, 2015
    Posts:
    3
    @Mecanim-Dev Although we can't enable/disable at runtime, is there a way to do it via an editor script? I currently have a validation script to ensure specific behaviours are added to AnimatorStates to all character animators in a given folder. I can add them if they don't exist, that's fine. But if the behaviour script is disabled is still appears in AnimatorState.behaviours, so it passes validation. Can I detect if the script is enabled?