using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; namespace SC.XR.Unity.Module_InputSystem { public class Module_InputSystem : SCModuleMono { public static Module_InputSystem instance { get; private set; } public event Action initializeCallBack; public bool initialize {get; private set;} static int inputSystemAwakeCount = 0; public event Action InputDeviceChangeCallBack; /// /// 所有支持的InputDevice,支持然后register后则启用 /// public InputDeviceBase[] _inputDeviceSupportList; public InputDeviceBase[] inputDeviceSupportList { get { if(_inputDeviceSupportList == null || _inputDeviceSupportList.Length==0) { _inputDeviceSupportList = GetComponentsInChildren(true); } return _inputDeviceSupportList; } } [Header("Enable Head")] [SerializeField] private bool activeHead = true; [Header("Enable BT3Dof")] [SerializeField] private bool activeBT3Dof = false; [Header("Enable KS")] [SerializeField] private bool activeKS = false; [Header("Enable GreyHand")] [SerializeField] private bool activeGGT26Dof = false; public override void OnSCAwake() { base.OnSCAwake(); inputSystemAwakeCount++; DebugMy.Log("InputSystem Awake Count :" + inputSystemAwakeCount, this); if(instance != null) { DestroyImmediate(this); return; } instance = this; foreach(var inputDevice in inputDeviceSupportList) { AddModule(inputDevice); } initialize = true; initializeCallBack?.Invoke(); DebugMy.Log("InputSystem Init Finish !", this); //StartCoroutine(WaitForInitFinish()); } public override void OnSCStart() { base.OnSCStart(); SetActiveInputDevice(InputDeviceType.Head, activeHead); SetActiveInputDevice(InputDeviceType.BT3Dof, activeBT3Dof); SetActiveInputDevice(InputDeviceType.KS, activeKS); SetActiveInputDevice(InputDeviceType.GGT26Dof, activeGGT26Dof); } //IEnumerator WaitForInitFinish() { // initialize = true; // initializeCallBack?.Invoke(); // DebugMy.Log("InputSystem Init Finish !", this); // yield break; //} public T GetInputDevice(InputDeviceType type) where T:InputDeviceBase { foreach(var inputDevice in inputDeviceSupportList) { if(type == inputDevice.inputDeviceType) { return (T)inputDevice; } } return null; } public void SetActiveInputDevice(InputDeviceType type,bool active) { InputDeviceBase inputDevice = GetInputDevice(type); if(inputDevice == null) return; SetFlag(type, active); if(active) { inputDevice.ModuleStart(); } else { inputDevice.ModuleStop(); } InputDeviceChangeCallBack?.Invoke(inputDevice, active); } private void SetFlag(InputDeviceType type, bool active) { if(type == InputDeviceType.Head) { activeHead = active; } else if(type == InputDeviceType.BT3Dof) { activeBT3Dof = active; } else if(type == InputDeviceType.KS) { activeKS = active; }else if(type == InputDeviceType.GGT26Dof) { activeGGT26Dof = active; } } InputDeviceBase _inputDevice; public bool GetInputDeviceStatus(InputDeviceType type) { _inputDevice = GetInputDevice(type); if(_inputDevice) { foreach(var part in _inputDevice.inputDevicePartList) { if(part.inputDataBase.isVaild == true) { return true; } } } return false; } public bool IsSomeDeviceActiveWithoutHead { get { if(GetInputDeviceStatus(InputDeviceType.GGT26Dof) || GetInputDeviceStatus(InputDeviceType.KS) || GetInputDeviceStatus(InputDeviceType.BT3Dof) ) { return true; } return false; } } } }