Module_InputSystem.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. using SC.XR.Unity.Module_Device;
  9. namespace SC.XR.Unity.Module_InputSystem {
  10. public class Module_InputSystem : SCModuleMono {
  11. public static Module_InputSystem instance { get; private set; }
  12. public event Action initializeCallBack;
  13. public bool initialize {get; private set; }
  14. public bool IsRunning { get; private set; }
  15. public event Action<InputDeviceBase, bool> InputDeviceChangeCallBack;
  16. /// <summary>
  17. /// 所有支持的InputDevice,支持然后register后则启用
  18. /// </summary>
  19. private List<InputDeviceBase> _inputDeviceSupportList;
  20. public List<InputDeviceBase> inputDeviceSupportList {
  21. get {
  22. if(_inputDeviceSupportList == null) {
  23. _inputDeviceSupportList = new List<InputDeviceBase>();
  24. InputDeviceBase[] inputDeviceList = GetComponentsInChildren<InputDeviceBase>(true);
  25. if (API_Module_Device.Current != null) {
  26. foreach (var inputDevice in inputDeviceList) {
  27. if (API_Module_Device.Current.SupportInputDevices.Contains(inputDevice.inputDeviceType)) {
  28. DebugMy.Log("Support InputDevice:" + inputDevice.inputDeviceType, this, true);
  29. _inputDeviceSupportList.Add(inputDevice);
  30. }
  31. }
  32. } else {
  33. _inputDeviceSupportList = inputDeviceList.ToList();
  34. }
  35. if (_inputDeviceSupportList.Count == 0) {
  36. DebugMy.Log("No Support InputDevice !", this, true);
  37. }
  38. }
  39. return _inputDeviceSupportList;
  40. }
  41. }
  42. //[Header("Enable Head")]
  43. //[SerializeField]
  44. private bool activeHead = true;
  45. //[Header("Enable BT3Dof")]
  46. //[SerializeField]
  47. private bool activeBT3Dof = false;
  48. //[Header("Enable KS")]
  49. //[SerializeField]
  50. private bool activeKS = false;
  51. //[Header("Enable GreyHand")]
  52. //[SerializeField]
  53. private bool activeGGT26Dof = false;
  54. public override void OnSCAwake() {
  55. base.OnSCAwake();
  56. if (instance != null) {
  57. DestroyImmediate(gameObject);
  58. return;
  59. }
  60. //DontDestroyOnLoad(gameObject);
  61. instance = this;
  62. DebugMy.Log("Awake", this, true);
  63. foreach (var inputDevice in inputDeviceSupportList) {
  64. AddModule(inputDevice);
  65. }
  66. initialize = true;
  67. IsRunning = true;
  68. initializeCallBack?.Invoke();
  69. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveHead")) {
  70. activeHead = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveHead", 0);
  71. }
  72. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveBT3Dof")) {
  73. activeBT3Dof = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveBT3Dof", 0);
  74. }
  75. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveKS")) {
  76. activeKS = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveKS", 0);
  77. }
  78. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ActiveGGT26Dof")) {
  79. activeGGT26Dof = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ActiveGGT26Dof", 0);
  80. }
  81. activeKS = true;
  82. }
  83. public override void OnSCStart() {
  84. base.OnSCStart();
  85. SetActiveInputDevice(InputDeviceType.Head, activeHead);
  86. SetActiveInputDevice(InputDeviceType.BT3Dof, activeBT3Dof);
  87. SetActiveInputDevice(InputDeviceType.KS, activeKS);
  88. SetActiveInputDevice(InputDeviceType.GGT26Dof, activeGGT26Dof);
  89. initialize = true;
  90. IsRunning = true;
  91. }
  92. public override void OnSCDisable() {
  93. base.OnSCDisable();
  94. initialize = false;
  95. IsRunning = false;
  96. }
  97. public T GetInputDevice<T>(InputDeviceType type) where T:InputDeviceBase {
  98. foreach(var inputDevice in inputDeviceSupportList) {
  99. if(type == inputDevice.inputDeviceType) {
  100. return (T)inputDevice;
  101. }
  102. }
  103. return null;
  104. }
  105. public void SetActiveInputDevice(InputDeviceType type,bool active) {
  106. InputDeviceBase inputDevice = GetInputDevice<InputDeviceBase>(type);
  107. if(inputDevice == null)
  108. return;
  109. SetFlag(type, active);
  110. if(active) {
  111. inputDevice.ModuleStart();
  112. } else {
  113. inputDevice.ModuleStop();
  114. }
  115. InputDeviceChangeCallBack?.Invoke(inputDevice, active);
  116. }
  117. private void SetFlag(InputDeviceType type, bool active) {
  118. if(type == InputDeviceType.Head) {
  119. activeHead = active;
  120. } else if(type == InputDeviceType.BT3Dof) {
  121. activeBT3Dof = active;
  122. } else if(type == InputDeviceType.KS) {
  123. activeKS = active;
  124. }else if(type == InputDeviceType.GGT26Dof) {
  125. activeGGT26Dof = active;
  126. }
  127. }
  128. InputDeviceBase _inputDevice;
  129. public bool GetInputDeviceStatus(InputDeviceType type) {
  130. _inputDevice = GetInputDevice<InputDeviceBase>(type);
  131. if(_inputDevice) {
  132. foreach(var part in _inputDevice.inputDevicePartList) {
  133. if(part.inputDataBase.isVaild == true) {
  134. return true;
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. public bool IsSomeDeviceActiveWithoutHead {
  141. get {
  142. if(GetInputDeviceStatus(InputDeviceType.GGT26Dof) ||
  143. GetInputDeviceStatus(InputDeviceType.KS) ||
  144. GetInputDeviceStatus(InputDeviceType.BT3Dof)
  145. ) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. }
  151. }
  152. }