/* * VideoKit * Copyright © 2025 Yusuf Olokoba. All Rights Reserved. */ #nullable enable namespace VideoKit.Internal { using System; using System.Collections; using UnityEngine; /// /// Event manager for hooking into Unity events. /// [DefaultExecutionOrder(-9_000)] public sealed class VideoKitEvents : MonoBehaviour { #region --Client API-- /// /// Event invoked at the end of each application frame. /// public event Action? onFrame; /// /// Event invoked on each Unity update. /// public event Action? onUpdate; /// /// Event invoked on each late Unity update. /// public event Action? onLateUpdate; /// /// Event invoked when the application is paused. /// public event Action? onPause; /// /// Event invoked when the application is resumed. /// public event Action? onResume; /// /// Event invoked when application is quit. /// public event Action? onQuit; /// /// VideoKit events instance. /// public static VideoKitEvents Instance => OptionalInstance = !ReferenceEquals(OptionalInstance, null) ? OptionalInstance : new GameObject(@"VideoKitEvents").AddComponent(); /// /// VideoKit events instance. /// public static VideoKitEvents? OptionalInstance { get; private set; } #endregion #region --Operations-- private void Awake () => DontDestroyOnLoad(gameObject); private IEnumerator Start () { var yielder = new WaitForEndOfFrame(); for (;;) { yield return yielder; onFrame?.Invoke(); } } private void Update () => onUpdate?.Invoke(); private void LateUpdate () => onLateUpdate?.Invoke(); private void OnApplicationPause (bool paused) => (paused ? onPause : onResume)?.Invoke(); private void OnApplicationQuit () { onQuit?.Invoke(); Destroy(gameObject); OptionalInstance = null; } #endregion } }