123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- 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<InputDeviceBase, bool> InputDeviceChangeCallBack;
- /// <summary>
- /// 所有支持的InputDevice,支持然后register后则启用
- /// </summary>
- public InputDeviceBase[] _inputDeviceSupportList;
- public InputDeviceBase[] inputDeviceSupportList {
- get {
- if(_inputDeviceSupportList == null || _inputDeviceSupportList.Length==0) {
- _inputDeviceSupportList = GetComponentsInChildren<InputDeviceBase>(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<T>(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<InputDeviceBase>(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<InputDeviceBase>(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;
- }
- }
- }
- }
|