// Animancer // Copyright 2020 Kybernetik // #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value. using UnityEngine; namespace Animancer.Examples.Events { /// /// An that uses a to recenve an Animation Event /// with the function name "Event". /// [AddComponentMenu(Strings.MenuPrefix + "Examples/Golf Events - Animation Simple")] [HelpURL(Strings.APIDocumentationURL + ".Examples.AnimationEvents/GolfHitControllerAnimationSimple")] public sealed class GolfHitControllerAnimationSimple : GolfHitController { /************************************************************************************************************************/ [SerializeField] private SimpleEventReceiver _EventReceiver; /************************************************************************************************************************/ /// /// Calls the base method and register /// to be called whenever the swing animation ends. /// /// Normally Animancer could call the registered method at the End Time defined in the transition, but in this /// case the used with this script has an Animation Event with the Function Name /// "End", which will execute the registered method when that event time passes. /// protected override void Awake() { base.Awake(); _Swing.Events.Sequence.OnEnd = EndSwing; } /************************************************************************************************************************/ /// /// After starting the swing animation, we also need to give the the callback /// that we want it to trigger when it receives an Animation Event with the Function Name "Event". /// /// In this case since there is only one animation with that event we could register it in /// without tieing it to a specific state, but normally the point of a is to /// allow multiple scripts to register their own callback for whatever animation they are playing. /// protected override void StartSwing() { base.StartSwing(); var state = _Animancer.States.Current; // When the Animation Event with the function name "Event" occurs: // If the swing animation doesn't have an event with that function name, this will log a warning. _EventReceiver.onEvent.Set(state, (animationEvent) => HitBall()); } /************************************************************************************************************************/ } }