Search Unity

Creating animation events (with EditorWindow)

Discussion in 'Immediate Mode GUI (IMGUI)' started by FeastSC2, Jul 11, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I'm trying to add events to animation clips (with a gameObject as argument).

    Code (CSharp):
    1.  
    2. var e = new AnimationEvent();
    3. e.functionName = "EventHitbox";
    4. e.objectReferenceParameter = hitbox.gameObject;
    5.  
    6. // Option 1: This seems to create everything as it should but once I run the game the events
    7. //are not fired properly (some of them work, some don't). I think this is closer to the "official"
    8. //way of going about it, but it won't work for me.
    9. var currentEvents = AnimationUtility.GetAnimationEvents(animClip).ToList();
    10. currentEvents.Add(e);
    11. AnimationUtility.SetAnimationEvents(animClip, currentEvents.ToArray());
    12.  
    13. // Option 2:
    14. animClip.AddEvent(e); // this is not persistent, once exitting playmode the events are gone.
    15. var currentEvents = AnimationUtility.GetAnimationEvents(animClip).ToList();
    16. // so I try and add it here permanently. This makes the anim events persist up until I restart Unity.
    17. // Additionally, the events in playmode are fired properly this way unlike Option 1.
    18. AnimationUtility.SetAnimationEvents(animClip, currentEvents.ToArray());
    19.  
    20.  
    So right now, I'm using option 2, but the problem of animation events disappearing is also true for Option 1, so it's all the same.

    TL;DR:
    My animation events references are gone once I restart Unity's application (the passed gameObject = null). Why does that happen?

    If you don't understand the problem, consider telling me how you would approach creating animation events in animation clips programmatically.
     
    Last edited: Jul 12, 2017
  2. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Okay I understood the issue. It's due to the fact that I was referencing game objects that were not Prefabs but gameObjects in the scene.

    Unfortunately, I don't believe that one can link animation events to scene objects.
     
    Last edited: Jul 12, 2017