using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; using System.Linq; namespace BITKit { [System.Serializable] public class InputActionGroup : IDisposable { int lockFile = Guid.NewGuid().GetHashCode(); public bool allowGlobalActivation = true; public bool isEnabled; InitializationState state = InitializationState.None; public ValidHandle allowInput = new(); List actions = new(); public InputActionGroup RegisterCallback(InputActionReference reference, Action callback) { if (reference is null) { Debug.LogWarning($"未知的引用"); return this; } EnsureConfiguration(); var action = reference.action.Clone(); actions .Where(x => x.name == action.name) .CreateOrAddIfEmety(actions, action) .ForEach(x => { x.RegisterCallback(callback); }); return this; } public void UnRegisterCallback(InputActionReference reference, Action callback) { foreach (var action in actions.Where(x => x.name == reference.action.name)) { action.UnRegisterCallback(callback); } } void EnsureConfiguration() { if (state is not InitializationState.Initialized) { Init(); state = InitializationState.Initialized; } } void Init() { if (allowGlobalActivation) BITInputSystem.AllowInput.AddListener(Listen); allowInput.AddListener(Allow); } void Listen(bool allowInput) { this.allowInput.SetElements(lockFile, allowInput); this.allowInput.SetDisableElements(lockFile, !allowInput); } void Allow(bool allow) { foreach (var action in actions) { if (allow) { action.Enable(); } else { action.Disable(); } } isEnabled = allow; } public void Dispose() { foreach (var action in actions) { action.Disable(); } actions.Clear(); } } }