0
\$\begingroup\$

I have an animation with animation events in an imported .fbx file. I spent some hours creating a class with delegates so that I could trigger methods on any C# class I wanted.

Now I wonder if that is necessary? Which types and methods does the animation event delegate a call to? Does it search through all fields, properties, containers etc in the GameObject with the Animator to find matching function names?

I found out unity has released a read-only version of their repository on github so I will dive in there and see if I can find what happens.

Ok I think it looks through all MonoBehavior based off the GetComponents on the GameObject with the Animator.

\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

Animation events use the SendMessage system. That means that when an Animator component encounters an animation event, then it goes through all the components on the same gameObject looking for methods with a specific name and calls them.

So all you need is a script on said gameObject with a method with the right name and (optionally) the right parameter. As with all the other Unity event methods (like Start or Update), those don't necessarily need to be public.

How you then proceed from there is up to you. For example, you might want to handle the animation event in a different behavior on a different gameObject, so you could translate it into a C# event. Or perhaps you want to feed into the newer Unity Event System. Or you could feed it into some event handling system of your own.

\$\endgroup\$
0
\$\begingroup\$

My interpretation is that this code sets up the function calls: https://github.com/Unity-Technologies/UnityCsReference/blob/2019.4.0f1/Editor/Mono/Animation/AnimationWindow/AnimationWindowEventInspector.cs

In there it looks like the GameObject's GetComponents method is called. So that is probably how it works.

(I haven't read the entire context so not entirely sure but it sounds reasonable).

\$\endgroup\$
4
  • \$\begingroup\$ Can you include the relevant excerpt of the linked code in your answer, so your reasoning isn't lost to future audiences if the repository ever changes URL or goes offline? \$\endgroup\$
    – DMGregory
    Commented Jun 12, 2020 at 11:44
  • \$\begingroup\$ I don't want to risk it, it was hard to understand the copyright notice. Also, I didn't find any "playmode loop", so I am not sure this is the right place to look. But it was a pretty straightforward function which iterated over GetComponents and used reflection to check the function signatures. "CollectSupportedMethods" is the one I looked at. \$\endgroup\$
    – Emil
    Commented Jun 12, 2020 at 14:54
  • 1
    \$\begingroup\$ When you have a theory, then you could just try it out to see if its correct or incorrect. \$\endgroup\$
    – Philipp
    Commented Nov 9, 2020 at 9:20
  • \$\begingroup\$ And then it fails suddenly in production because maybe the theory was wrong. \$\endgroup\$
    – Emil
    Commented Nov 10, 2020 at 8:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .