diff --git a/Runtime/SkyHookManager.cs b/Runtime/SkyHookManager.cs index 99aa05d..c23304b 100644 --- a/Runtime/SkyHookManager.cs +++ b/Runtime/SkyHookManager.cs @@ -3,87 +3,105 @@ using UnityEngine.Events; namespace SkyHook { - public class SkyHookManager : MonoBehaviour - { - private static SkyHookManager _instance; - - internal static bool IsFocused; - - public bool requireFocus = true; - - private bool _started; - - public static UnityEvent KeyUpdated = new(); - - public static SkyHookManager Instance + public class SkyHookManager : MonoBehaviour { - get - { - if (_instance) return _instance; + private static SkyHookManager _instance; - var obj = new GameObject("SkyHook Manager"); + private static bool IsFocused; - _instance = obj.AddComponent(); + /// + /// Whether or not the event will be received only if the game window is focused. + /// + // ReSharper disable once MemberCanBePrivate.Global + // ReSharper disable once FieldCanBeMadeReadOnly.Global + public static bool RequireFocus = true; - DontDestroyOnLoad(_instance); + private bool _started; - return _instance; - } + /// + /// The key updated event data + /// + // ReSharper disable once MemberCanBePrivate.Global + public static readonly UnityEvent KeyUpdated = new(); + + /// + /// The instance of sky hook manager. The instance will be crated if it does not exist + /// + // ReSharper disable once MemberCanBePrivate.Global + public static SkyHookManager Instance + { + get + { + if (_instance) return _instance; + + var obj = new GameObject("SkyHook Manager"); + + _instance = obj.AddComponent(); + + DontDestroyOnLoad(_instance); + + return _instance; + } + } + + private void HookCallback(SkyHookEvent ev) + { + if (RequireFocus && !IsFocused) + { + return; + } + + KeyUpdated.Invoke(ev); + } + + private void StartHook() + { + var result = SkyHookNative.StartHook(HookCallback); + + if (result != null) + { + throw new SkyHookException(result); + } + + _started = true; + } + + private void StopHook() + { + if (!_started) return; + + var result = SkyHookNative.StopHook(); + + if (result != null) + { + throw new SkyHookException(result); + } + + _started = false; + } + + public static void Start() + { + Instance.StartHook(); + } + + public static void Stop() + { + Instance.StopHook(); + } + + private void OnDestroy() + { + Debug.Log("Destroy"); + StopHook(); + } + + private void Run() + { + if (requireFocus) + { + IsFocused = Application.isFocused; + } + } } - - private void HookCallback(SkyHookEvent ev) - { - KeyUpdated.Invoke(ev); - } - - private void StartHook() - { - var result = SkyHookNative.StartHook(HookCallback); - - if (result != null) - { - throw new SkyHookException(result); - } - - _started = true; - } - - private void StopHook() - { - if (!_started) return; - - var result = SkyHookNative.StopHook(); - - if (result != null) - { - throw new SkyHookException(result); - } - - _started = false; - } - - public static void Start() - { - Instance.StartHook(); - } - - public static void Stop() - { - Instance.StopHook(); - } - - private void OnDestroy() - { - Debug.Log("Destroy"); - StopHook(); - } - - private void Run() - { - if (requireFocus) - { - IsFocused = Application.isFocused; - } - } - } } \ No newline at end of file