Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

What is the implementation logic of AnimationClip.AddEvent?

Discussion in 'Animation' started by xuzhuoxi, Sep 24, 2022.

  1. xuzhuoxi

    xuzhuoxi

    Joined:
    Aug 11, 2020
    Posts:
    3
    Code (CSharp):
    1.         public static AnimationEvent AddCustomEvent(this AnimationClip clip, float seconds, string eventName)
    2.         {
    3.             var evt = new AnimationEvent
    4.             {
    5.                 functionName = "MyFuncName",
    6.                 stringParameter = eventName,
    7.                 time = seconds
    8.             };
    9.             clip.AddEvent(evt);
    10.             var events = clip.events;
    11.             foreach (var e in events)
    12.             {
    13.                 DebugUtil.Log("Foreach:", evt == e);
    14.             }
    15.  
    16.             return evt;
    17.         }
    The print result is

    Foreach: False
    Foreach: False
    Foreach: False



    Why? What is the implementation logic of AnimationClip.AddEvent?
     

    Attached Files:

    • shot.PNG
      shot.PNG
      File size:
      11.9 KB
      Views:
      268
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,605
    It probably makes a copy of the event. Compare their functionName.
     
  3. xuzhuoxi

    xuzhuoxi

    Joined:
    Aug 11, 2020
    Posts:
    3
    Thanks. I want to figure out what the code designer thinks. And there may be more than one event using functionName, which may remove all or an incorrect one
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,605
    If adding an event added it directly by reference, you would be able to modify it without the clip knowing. For example, in the event system I made in Animancer events must always be sorted by time for efficiency. Each frame it checks the next event and if its time has passed then it checks the next event but if its time hasn't passed then it knows no other event has passed either so it doesn't have to check every event every frame. But if you could add an event by reference then set its time, it could be out of order for the new time so the system wouldn't work properly. I expect Unity's system works similarly.

    If you have multiple events with the same name, then you'll just need to differentiate them with other parameters as well.
     
  5. xuzhuoxi

    xuzhuoxi

    Joined:
    Aug 11, 2020
    Posts:
    3
    Unity behaves similar to what you describe because the results returned by the Animation.events property are in chronological order. In my opinion, the Event added in the Editor does not need to be supported by RemoveEvent, but if it is added by script, I think there should be corresponding RemoveEvent behavior support. I've always wondered how Unity handles the need for RemoveEvent, because this problem has been around for a long time.