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

Looking For Best Practices with Animation Events (multiple parameters)

Discussion in 'Animation' started by BillDNA, Jun 6, 2021.

  1. BillDNA

    BillDNA

    Joined:
    Jun 3, 2014
    Posts:
    7
    So I'm trying to figure out the best way to implement animation events that take in multiple parameters, Much to my disappointment Unity dose not let you use multiple parameters or custom structs. Currently I'm looking at three different workarounds.
    1) having a dictionary <string, paramStruct> that then the animator can call passing in the string.
    2) Creating a scriptable object with all the params on in and using the AnimationEvent class as the param and setting the object to that scriptable object.
    3) Create a method for every variation on parameters

    --- A more specific example ---

    I want to call a method with this signature
    Code (CSharp):
    1. public void SetPoseFor(ResolutionActor actor, CharacterPose pose)
    Both ResolutionActor and CharacterPose are enums.
    Actor is dynamic based on game state (effectively a Dictionary<ResolutionActor,SpriteAtlas>)
    Pose is a sprite in SpriteAtlas.

    The method will update the sprite render with a new sprite. The sprite can't be set in the animator because players might have different skins(sprite atlases) set.

    --- Issues with Work Arounds ---

    Because there are currently 6 different posable actors and 8 poses, thats 48 entries for the Dictionary or Scriptable objects, which will be a mess to deal with especially when more poses get added.

    --- What I'm Looking For ---

    In an ideal world some magic assets on the asset store allow just use use multiple parameters or a custom struct.

    Baring that some way to extend the Animation Event inspector window so I can build some in line editing for the animator to use.

    Any sort of solution you can think of to this, Keeping in mid the goal to allow the animation clip to trigger a variety dynamic events.

    Thanks
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,554
    • There's no way to modify the Animation Event Inspector and to store two enum values you'd also need to modify the actual data it serializes in the AnimationClip which is even less possible.
    • You can give your method an AnimationEvent parameter which gives you access to all the parameter types (int, string, etc.), but that allocates garbage every time the event is triggered so I wouldn't recommend it.
    • Probably the best option for Animation Events would be to make a ScriptableObject class containing all the fields you need and send that in using the object reference parameter. That would be almost as good as the struct approach you wanted, except that the Scriptable Objects would be saved as separate assets from the animations so it would be a bit more hassle to manage.
    • Better yet, Animancer (link in my signature) has a Custom Event System which lets you set up events using UnityEvents in the Inspector and can be made to use UltEvents instead (another one of my plugins, basically just better UnityEvents with support for calling methods with any number of parameters, more parameter types including enums, and various other improvements).