- Home /
One parameter limit on animation event functions temporary?
Is the limitation on the number of parameters for animation event function calls (currently, only one parameter functions are exposed) temporary? I have several cases where multiple parameters would make my life much easier, and wondering if I should go ahead and work out hacks around this, or delay those for an update.
Answer by duck · Feb 07, 2010 at 03:14 PM
I don't see any indication that this would be a temporary implementation. I think because the Animation Event can take an "Object Reference" as the parameter, this essentially allows you to pass any kind and amount of data you like.
Simply put the data into an array, or create your own custom class to contain the data and pass an instance of that. If that's what you were considering doing already, there's no need to think of it as a "hack" - it's common practice!
It's common practice because it has to be, and I'd call it a hack for the simple reason that it's a hack (being forced to do it doesn't change it's level of elegance). I agree that it can allow the function to do anything, just not in a clean or efficient way, which is what makes using the animation event so attractive to me.
Thanks for the response, but I'm looking for an official one. I agree that there's no indication it's temporary, but also none that it's permanent, which is why I'm asking.
I understand that it can be a matter of subjective aesthetic, but to me, a custom class or struct designed to carry a collection of related data often seems more elegant than having that data split across an arbitrary number of parameters. However, I concede that it still may be the case your particular requirements (and since you haven't described them, I can only speculate) may be more suited to multiple parameters. I hope you find a more satisfactory answer! :-)
Upon further thought, I'd agree with you, if there was an option for that class to be instanced and managed by the Animation Event, and saved with the animation. The only way I've gotten it to work is by attaching a script to a gameobject, and referencing that, which is very clunky to manage, since to review/change settings you have to select the game object, deselecting what you're animating.
I'd say that if I could have animation event create/manage an instance, and have an inspector for it at the bottom of the window, that'd be best, wouldn't break animations by adding/changing params.
for code reference check this http://answers.unity3d.com/questions/722410/animation-events-how-is-the-parameter-animationeve.html
Something may have changed with the API for Animation Events. As you can't pass an object reference anymore.
Answer by Tommyph1208 · Jan 24, 2014 at 01:40 PM
Reviving this for people like me who may stumble upon it on Google... Now (maybe also back then) you can pass an AnimationEvent in the function called by your animation event... Sounds funky I know... The animation even is a collection of info and includes:
an int a float a string and an object reference
Answer by Ash-Blue · Jan 24, 2016 at 08:15 AM
After some testing it looks like Unity only allows AnimationEvent, int, float, and string. You cannot pass in an object reference as mentioned in another answer. That said, here are your options.
The "easiest" solution at the moment for multiple parameters is to use an AnimationEvent, this is pretty inefficient if you need multiple ints, float, strings, or other complex data. Details: http://docs.unity3d.com/ScriptReference/AnimationEvent.html
Parse a string with multiple values separated by a comma (slow, messy, and error prone).
Write individual functions for every event you need (circumventing params). Very messy for complex events.
Use setters to alter the data in events, could result in a ton of events and more mess.
In short there is no good solution until Unity expands on the animation window's serialization API. Best of luck.
u should've also included, Object ref as a option. As it can include all data that a player might need. And , i don't think a single animation event was designed to multiple data changes. I mean to say if in a player atk animation , a collider hits enemy mid-way then that should call OnCollisionEnter() insted of using anim-event at such place. Basically, imo it is made to do small trigger events .
Answer by Serinx · Jun 10, 2018 at 11:00 PM
I don't know if this is correct but what I did is create a MyAnimationEvents class, this class holds all of the event information I need (damage, radius, prefabs, names etc). I made prefabs of all the various events which only have this script attached e.g.
animEventStandardAttack - damage = 1f, radius = 1f, particleSystem = blood, name = "standard"
animEventHeavyAttack - damage = 2f, radius = 2f, particleSystem = bigBlood, name = "heavy"
When I call the animation event, I pass in the prefab I want. The function instantiates the prefab, grabs the values from it and uses them as necessary. Heres the code I'm using in the animation event:
public void DoDamage(GameObject animEventGO)
{
MyAnimationEvent animEvent = GameObject.Instantiate(animEventGO).GetComponent<MyAnimationEvent>();
if (animEvent.damage == 0) animEvent.damage = 1f;
if (animEvent.radius == 0) animEvent.radius = 1f;
MeleeWeapon wep = gameObject.GetComponent<Inventory>().equippedItem.GetComponent<MeleeWeapon>();
wep.DoDamage(animEvent.damage, animEvent.radius);
}
Hope this helps someone!
Answer by nanocba83 · Jul 14, 2018 at 03:13 AM
Inspired by @Serinx answer, this is how I'm doing it now:
Create an animation event class with the information you need
Allow that class to be used to create assets out of it by adding [CreateAssetMenu (menuName = your_menu_name)] on top of its declaration
Create as many animation events as you wish by right-clicking on your project and choosing the animation event from the corresponding menu
On the script you have the event handler, you can now write something like this:
public void OnAnimationEvent(MyAnimationEvent animationEvent) { //Use it directly, without the need of instantiating }
Your answer
