Module_InputSystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEngine;
  8. namespace SC.XR.Unity.Module_InputSystem {
  9. public class Module_InputSystem : SCModuleMono {
  10. public static Module_InputSystem instance { get; private set; }
  11. public event Action initializeCallBack;
  12. public bool initialize {get; private set;}
  13. static int inputSystemAwakeCount = 0;
  14. public event Action<InputDeviceBase, bool> InputDeviceChangeCallBack;
  15. /// <summary>
  16. /// 所有支持的InputDevice,支持然后register后则启用
  17. /// </summary>
  18. public InputDeviceBase[] _inputDeviceSupportList;
  19. public InputDeviceBase[] inputDeviceSupportList {
  20. get {
  21. if(_inputDeviceSupportList == null || _inputDeviceSupportList.Length==0) {
  22. _inputDeviceSupportList = GetComponentsInChildren<InputDeviceBase>(true);
  23. }
  24. return _inputDeviceSupportList;
  25. }
  26. }
  27. [Header("Enable Head")]
  28. [SerializeField]
  29. private bool activeHead = true;
  30. [Header("Enable BT3Dof")]
  31. [SerializeField]
  32. private bool activeBT3Dof = false;
  33. [Header("Enable KS")]
  34. [SerializeField]
  35. private bool activeKS = false;
  36. [Header("Enable GreyHand")]
  37. [SerializeField]
  38. private bool activeGGT26Dof = false;
  39. public override void OnSCAwake() {
  40. base.OnSCAwake();
  41. inputSystemAwakeCount++;
  42. DebugMy.Log("InputSystem Awake Count :" + inputSystemAwakeCount, this);
  43. if(instance != null) {
  44. DestroyImmediate(this);
  45. return;
  46. }
  47. instance = this;
  48. foreach(var inputDevice in inputDeviceSupportList) {
  49. AddModule(inputDevice);
  50. }
  51. initialize = true;
  52. initializeCallBack?.Invoke();
  53. DebugMy.Log("InputSystem Init Finish !", this);
  54. //StartCoroutine(WaitForInitFinish());
  55. }
  56. public override void OnSCStart() {
  57. base.OnSCStart();
  58. SetActiveInputDevice(InputDeviceType.Head, activeHead);
  59. SetActiveInputDevice(InputDeviceType.BT3Dof, activeBT3Dof);
  60. SetActiveInputDevice(InputDeviceType.KS, activeKS);
  61. SetActiveInputDevice(InputDeviceType.GGT26Dof, activeGGT26Dof);
  62. }
  63. //IEnumerator WaitForInitFinish() {
  64. // initialize = true;
  65. // initializeCallBack?.Invoke();
  66. // DebugMy.Log("InputSystem Init Finish !", this);
  67. // yield break;
  68. //}
  69. public T GetInputDevice<T>(InputDeviceType type) where T:InputDeviceBase {
  70. foreach(var inputDevice in inputDeviceSupportList) {
  71. if(type == inputDevice.inputDeviceType) {
  72. return (T)inputDevice;
  73. }
  74. }
  75. return null;
  76. }
  77. public void SetActiveInputDevice(InputDeviceType type,bool active) {
  78. InputDeviceBase inputDevice = GetInputDevice<InputDeviceBase>(type);
  79. if(inputDevice == null)
  80. return;
  81. SetFlag(type, active);
  82. if(active) {
  83. inputDevice.ModuleStart();
  84. } else {
  85. inputDevice.ModuleStop();
  86. }
  87. InputDeviceChangeCallBack?.Invoke(inputDevice, active);
  88. }
  89. private void SetFlag(InputDeviceType type, bool active) {
  90. if(type == InputDeviceType.Head) {
  91. activeHead = active;
  92. } else if(type == InputDeviceType.BT3Dof) {
  93. activeBT3Dof = active;
  94. } else if(type == InputDeviceType.KS) {
  95. activeKS = active;
  96. }else if(type == InputDeviceType.GGT26Dof) {
  97. activeGGT26Dof = active;
  98. }
  99. }
  100. InputDeviceBase _inputDevice;
  101. public bool GetInputDeviceStatus(InputDeviceType type) {
  102. _inputDevice = GetInputDevice<InputDeviceBase>(type);
  103. if(_inputDevice) {
  104. foreach(var part in _inputDevice.inputDevicePartList) {
  105. if(part.inputDataBase.isVaild == true) {
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. public bool IsSomeDeviceActiveWithoutHead {
  113. get {
  114. if(GetInputDeviceStatus(InputDeviceType.GGT26Dof) ||
  115. GetInputDeviceStatus(InputDeviceType.KS) ||
  116. GetInputDeviceStatus(InputDeviceType.BT3Dof)
  117. ) {
  118. return true;
  119. }
  120. return false;
  121. }
  122. }
  123. }
  124. }