“This is the 25th day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

Two ways to add events

The first method is property panel. Note that in the previous version, click “Add event” and a window for registering an event pops up. Now, the window for registering an event is placed under the Inspector.

The second is code implementation: relatively speaking, the property panel can be more convenient and accurate control at what time to start the registered event, can pass 0 or 1 parameter, but the form of implementation is different, there is no essential difference

Code to achieve the registration of animation time reference code is as follows:

public class AnimationEventDemo : MonoBehaviour
{

    Animation ani;    // Declare Animation
    AnimationClip aniclip;     // Declare aniclip animation clip

    void Start () {
        ani = GetComponent<Animation>();   // Get the component

        aniclip = ani.GetClip("walk");     // Get the animation clip of Walk and assign it to Aniclip

        // Create an AnimationEvent object
        AnimationEvent anievent = new AnimationEvent();

        anievent.functionName = "Show";   // Register methods to events

        anievent.time = aniclip.length * 0.9 f;  // Sets the time when the event is triggered in the action

        anievent.intParameter = 5;        // Pass the corresponding parameters in the method

        aniclip.AddEvent(anievent);       // Add the set event to the animation

    }
	
    void Update () {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            ani.Play("walk");  // Play the walk action}}public void Show(int i)
    {
        Debug.Log("Call the registration method show with the argument:"+i); }}Copy the code

An error

‘GOName’ AnimationEvent ‘FunctionName’ has no receiver! Are you missing a component?


Error:

The first thing to do with this problem is to check to see if the method name is misspelled or if the method doesn’t exist in the code at all,

Or the reference of the listener on the Animation panel is lost. I checked the problem again, but still did not find the problem.

This I do not understand a little, remember before is so used ah, now why is not good to use it, it is very strange. However, I did not give up. I removed the event listener from the Animation panel and no error was reported when I ran it again.

But this is not my fundamental solution to the problem ah, I want the event callback is not ah, at this time, I use the code form to do this event monitor, part of the code is as follows:

 	AnimationClip PokerClip = PokerAnimation.GetClip(AniName);
        AnimationEvent AniEvt = new AnimationEvent();
        // This function needs to be mounted on the same body as the Animation.
        AniEvt.functionName = "OnPlayEndCallBack";
        // Set the execution time
        AniEvt.time = PokerClip.length - 0.28 f;
        // Register events
        PokerClip.AddEvent(AniEvt);
Copy the code

At this point, my problem has been solved, but the reason is not that adding code is good, and assigning on the panel is bad. The reason is that the script used to listen to the function needs to be mounted on the same game body as the Animation.

Since Animation is not often used, such a small problem has consumed nearly an hour of my time. I really regret it. Also hope that through this blog can give you encountered similar problems, bring some ideas or tips.