Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Get all events in Animations to loop, is possible?

Discussion in 'Scripting' started by bisewski, Nov 25, 2023.

  1. bisewski


    Jan 16, 2014

    Is possible to do a foreach in Events inserted in animations?

    I have some Events in my animation to stop in pre-defined frames...

    But by script I dont know where these Events are, in what frame is it.

    So if possible maybe a loop for each can solve. If was possible to use more then one variable in functions that Events can call will be nice because we can just set prev==10; actual==20; next==30; just for instance. But I tried and Events only accept one argument.

    The goal is jump to the next position or to the prev position.

    In my animation, I can forward or backward clicking in buttons "prev" or "next" that will play anim until next Event that will call a function that stop animation in that frame. But today my anim need to play until hit the next Event. I need also to jump to next Event.

    Thank you!
    Last edited: Nov 25, 2023
  2. JambonBurst6


    Nov 16, 2021
    Hi there!

    I'm not sure about what you're doing exactly, and I believe what you're trying to achieve should be done with something else that animation events.
    Still, here's an answer to your questions, if this can help.

    It seems possible to loop through all events of an AnimationClip, using this code (I did not test it, but I suppose it works fine since there's nothing special about it). Of course you don't want to use new() for an AnimationClip, you want to reference the desired clip another way, this is just to show how the loop looks.

    Code (CSharp):
    1. AnimationClip clip = new AnimationClip();
    2. foreach (AnimationEvent clipEvent in
    3. {
    4.     Debug.Log(clipEvent.time);
    5.     Debug.Log(clipEvent.functionName);
    6.     // etc.
    7. }

    About an event having more than 1 parameter, to my knowledge this is not possible. What you can do is use a ScriptableObject as parameter, and in this ScriptableObject you can have as many parameters as you want.
    So for instance, your ScriptableObject could look like this:

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Event Parameters", menuName = "Event Parameters")]
    2. public class EventParameters : ScriptableObject
    3. {
    4.     public float ParameterA;
    5.     public float ParameterB;
    6.     public float ParameterC;
    7. }
    And the listener method like this:

    Code (CSharp):
    1. public void SomeEventListener(EventParameters parameters)
    2. {
    3.     Debug.Log(parameters.ParameterA);
    4. }

    Once again, even though I don't understand your problem precisely, I'm pretty sure animation events are not what you'd want to use, but I hope this can help.
    bisewski likes this.
  3. bisewski


    Jan 16, 2014

    This is because my english is so bad that complicate everything...

    But you are right, With your code I can get all Events in one clip. So I can get one specific Event and get the specific time. Using Play we can stop at this time.

    The all idea about what I tried to explain is basically to create one similar tool like PowerPoint. We have slides in PowerPoint. Each slide is basically one picture. So the all presentation is a compilation of images. To do it in unity is very simple...

    But instead of we have one compilation of images, if we had one single animation like a "step by step". So we stop at every step and only start the next step when click in "play" or "next" button again.

    Ok, we can do each of these steps individual and call with triggers. But in some cases is much more simple to use one single animation and stopping at specific frames.

    But you show the path...Thank you Sr...