Module_InputSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  82. public override void OnSCStart() {
  83. base.OnSCStart();
  84. SetActiveInputDevice(InputDeviceType.Head, activeHead);
  85. SetActiveInputDevice(InputDeviceType.BT3Dof, activeBT3Dof);
  86. SetActiveInputDevice(InputDeviceType.KS, activeKS);
  87. SetActiveInputDevice(InputDeviceType.GGT26Dof, activeGGT26Dof);
  88. }
  89. public override void OnSCDisable() {
  90. base.OnSCDisable();
  91. initialize = false;
  92. IsRunning = false;
  93. }
  94. public T GetInputDevice<T>(InputDeviceType type) where T:InputDeviceBase {
  95. foreach(var inputDevice in inputDeviceSupportList) {
  96. if(type == inputDevice.inputDeviceType) {
  97. return (T)inputDevice;
  98. }
  99. }
  100. return null;
  101. }
  102. public void SetActiveInputDevice(InputDeviceType type,bool active) {
  103. InputDeviceBase inputDevice = GetInputDevice<InputDeviceBase>(type);
  104. if(inputDevice == null)
  105. return;
  106. SetFlag(type, active);
  107. if(active) {
  108. inputDevice.ModuleStart();
  109. } else {
  110. inputDevice.ModuleStop();
  111. }
  112. InputDeviceChangeCallBack?.Invoke(inputDevice, active);
  113. }
  114. private void SetFlag(InputDeviceType type, bool active) {
  115. if(type == InputDeviceType.Head) {
  116. activeHead = active;
  117. } else if(type == InputDeviceType.BT3Dof) {
  118. activeBT3Dof = active;
  119. } else if(type == InputDeviceType.KS) {
  120. activeKS = active;
  121. }else if(type == InputDeviceType.GGT26Dof) {
  122. activeGGT26Dof = active;
  123. }
  124. }
  125. InputDeviceBase _inputDevice;
  126. public bool GetInputDeviceStatus(InputDeviceType type) {
  127. _inputDevice = GetInputDevice<InputDeviceBase>(type);
  128. if(_inputDevice) {
  129. foreach(var part in _inputDevice.inputDevicePartList) {
  130. if(part.inputDataBase.isVaild == true) {
  131. return true;
  132. }
  133. }
  134. }
  135. return false;
  136. }
  137. public bool IsSomeDeviceActiveWithoutHead {
  138. get {
  139. if(GetInputDeviceStatus(InputDeviceType.GGT26Dof) ||
  140. GetInputDeviceStatus(InputDeviceType.KS) ||
  141. GetInputDeviceStatus(InputDeviceType.BT3Dof)
  142. ) {
  143. return true;
  144. }
  145. return false;
  146. }
  147. }
  148. }
  149. }