Gameplay.AnimationEventReceiver - robblofield/TomeboundDocs GitHub Wiki
Gameplay.AnimationEventReceiver
Author: Shahrzad Beevis
Last Updated: 14/05/2024
Overview
This script defines an event, which is triggered when an animation associated with the GameObject is finished.
Dependencies
- N/A
Contents
- Use case
- Breakdown of code
- Using Statements
- Class Declaration
- Other Declarations
- Method/AnimationComplete()
- Future Expansion
- Full Code Reference
Use case
This script can be used to synchronise actions with animations in Unity; for instance, triggering specific behaviours or events when an animation completes, such as advancing to the next step in a sequence or activating certain effects.
Breakdown of Code
Using Statements
using UnityEngine;
Class Declaration
public class AnimationEventReceiver : MonoBehaviour
Other Declarations
public delegate void AnimationCompleteDelegate();
public event AnimationCompleteDelegate OnAnimationComplete;
public delegate void AnimationCompleteDelegate():
This line declares a delegate type named AnimationCompleteDelegate. This delegate type represents a method signature that does not take any parameters and does not return any value (void). It defines the structure of methods that can be subscribed to an event.
public event AnimationCompleteDelegate OnAnimationComplete:
This line declares an event named OnAnimationComplete of type AnimationCompleteDelegate. This event allows other scripts or classes to subscribe to and be notified when an animation completes, triggering any methods that have been subscribed to this event.
AnimationComplete()
public void AnimationComplete()
{
OnAnimationComplete?.Invoke();
}
When invoked, this method first checks if there are any subscribers to the event using the null-conditional operator. If subscribers exist the Invoke() method is called. This action effectively executes all the methods that have been subscribed to the OnAnimationComplete event. Consequently, upon the completion of an animation associated with the AnimationEventReceiver GameObject, any custom behaviours or events tied to the OnAnimationComplete event are promptly executed, providing a means to synchronise actions with animation completion within Unity projects.
Future Expansion
N/A
Full Code Reference
using UnityEngine;
public class AnimationEventReceiver : MonoBehaviour
{
public delegate void AnimationCompleteDelegate();
public event AnimationCompleteDelegate OnAnimationComplete;
public void AnimationComplete()
{
OnAnimationComplete?.Invoke();
}
}