Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Getting the StateMachineBehaviour <-> owning state relation from code

Discussion in 'Animation' started by Ylisar, Oct 20, 2022.

  1. Ylisar

    Ylisar

    Joined:
    Jan 27, 2014
    Posts:
    18
    As for why it's currently needed for us. We have game play logic inside of StateMachineBehaviour ( henceforth SMB ), overall there's lots of upside to this approach vs trying to go with merely MonoBehaviour composition, this also seems to be an intended use case for SMB. As is fairly natural these SMBs then contain state information which is relevant to the related GameObject, and so if one wants to support serialization SMBs needs to be serialization aware.

    Now we could use something a long the lines of:
    Code (CSharp):
    1. m_Animator.GetBehaviours<CommonBaseSMB>().OfType< ISaveLoadState >()
    On the MonoBehaviour side of things, and as long as the order returned from
    GetBehaviours<CommonBaseSMB>()
    is consistent we could later restore the state by just making sure that each SMB reads as many bytes as it wrote. Now if there even are such guarantees there's a big down side to relying on the stream position, because if the state machine is ever changed this approach will fail. What's needed is that each SMB is able to provide a UID within the state machine of some sort which is persistent across runs and doesn't change for existing SMBs when the state machine is modified. Now it would seem this already exist:
    AnimatorStateInfo.fullPath

    But this relation does not seem to be available save for when the SMBs are active or involved in a transition, which makes it pretty much useless for this case. Is there anything I've missed here or is there something in the works?