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

Handling instancing/transitions of prefab at runtime, using StateMachineBehaviour

Discussion in 'Scripting' started by simoo, Sep 24, 2019.

  1. simoo

    simoo

    Joined:
    Jul 20, 2019
    Posts:
    12
    Hi,

    I'have a menu that is being filled at runtime, based on a previous selection.
    So I've a prefab that I instantiate as usual in a loop.

    Every prefab comes with an Animator that handles enter and exit transitions.

    What I'd like to achieve is the following, using StateMachineBehavior (almost c# pseudocode):

    Code (CSharp):
    1. // ENTER Animation
    2. onStateEnter() {
    3.     menuHandler.drawItems()
    4. }
    5.  
    6. // EXIT Animation
    7. onStateEnter() {
    8.     menuHandler.destroyItems()
    9. }
    10.  
    11. // menuHandler drawItems
    12. foreach(item in items) {
    13.     go = Instantiate(itemPrefab, position, rotation);
    14.     go.animator.trigger("Entering")
    15. }
    16.  
    17. // menuHandler destroyItems
    18. foreach(go in items) {
    19.     Destroy(go)
    }

    And then have this MACHINE STATES:

    • ENTER
    • ENTERING
    • EXITING
    • EXIT

    The -ing states are where the animation occurs.

    Is this the right approach to handle runtime children animations in a sensible way?
    Am I missing something else?

    StateMachineBehaviour reference: https://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html
     
    Last edited: Sep 24, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    I suggest you don't rely your logic on state machine behaviours. While it's okay for things like sfx/vfx, it will break your program because there are a lot of conditions for certain envents to be skipped. Also their order is messed. Next state enter may come out before current state exit, for instance.
     
    simoo likes this.
  3. simoo

    simoo

    Joined:
    Jul 20, 2019
    Posts:
    12
    Thanks palex-nx.
    I actually discarded the state machine behaviour idea.

    I ended up with this solution (almost pseudocode)

    Code (CSharp):
    1. transitionRunning = true
    2. targetState = "Exited"
    3. animatingCount = children.count
    4.  
    5. while(transitionRunning)
    6.  
    7.     foreach (child in children)
    8.  
    9.         if child.animator.GetCurrentAnimatorStateInfo(0).isName(targetState)
    10.             animatingCount -= 1
    11.  
    12.      if (animatingCount <= 0)
    13.         // dispatch the proper event
    14.         OnExitTransitionEvent.Raise()
    15.         transitionRunning = false
    16.      else
    17.         yield return WaitForEndOfFrame
    18.  
    Still wondering wether it is the correct approach (I'm new to Unity)
     
    Last edited: Sep 27, 2019
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Still none. Everyone invents it's own and noose succedeed to replace all of them. But there are three common approaches to animations serving different purposes. Those are Timeline, Animator and Playables.
     
  5. simoo

    simoo

    Joined:
    Jul 20, 2019
    Posts:
    12
    So? Is there any example or reference you can point?
     
  6. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    No, i cant point any decent example and say I suggest you do it this way. With multiple approaches you need to chose carefully what fits where in your game best.
     
  7. simoo

    simoo

    Joined:
    Jul 20, 2019
    Posts:
    12
    So you're telling me that my approach is not correct, and that there are three common approaches to do it.
    But you can't give me a reference to any of those?
     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    No im not telling yours is not correct, its pretty okay to handle animator this way, im telling you also there are timeline and playables, check may be they fit better for your game or some aspects of it