SteamVR_ControllerManager.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using UnityEngine;
  3. using Valve.VR;
  4. public class SteamVR_ControllerManager : MonoBehaviour
  5. {
  6. private void Awake()
  7. {
  8. int num = (this.objects == null) ? 0 : this.objects.Length;
  9. GameObject[] array = new GameObject[2 + num];
  10. this.indices = new uint[2 + num];
  11. array[0] = this.right;
  12. this.indices[0] = uint.MaxValue;
  13. array[1] = this.left;
  14. this.indices[1] = uint.MaxValue;
  15. for (int i = 0; i < num; i++)
  16. {
  17. array[2 + i] = this.objects[i];
  18. this.indices[2 + i] = uint.MaxValue;
  19. }
  20. this.objects = array;
  21. }
  22. private void OnEnable()
  23. {
  24. for (int i = 0; i < this.objects.Length; i++)
  25. {
  26. GameObject gameObject = this.objects[i];
  27. if (gameObject != null)
  28. {
  29. gameObject.SetActive(false);
  30. }
  31. }
  32. this.OnTrackedDeviceRoleChanged(new object[0]);
  33. for (int j = 0; j < SteamVR.connected.Length; j++)
  34. {
  35. if (SteamVR.connected[j])
  36. {
  37. this.OnDeviceConnected(new object[]
  38. {
  39. j,
  40. true
  41. });
  42. }
  43. }
  44. SteamVR_Utils.Event.Listen("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
  45. SteamVR_Utils.Event.Listen("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  46. SteamVR_Utils.Event.Listen("TrackedDeviceRoleChanged", new SteamVR_Utils.Event.Handler(this.OnTrackedDeviceRoleChanged));
  47. }
  48. private void OnDisable()
  49. {
  50. SteamVR_Utils.Event.Remove("input_focus", new SteamVR_Utils.Event.Handler(this.OnInputFocus));
  51. SteamVR_Utils.Event.Remove("device_connected", new SteamVR_Utils.Event.Handler(this.OnDeviceConnected));
  52. SteamVR_Utils.Event.Remove("TrackedDeviceRoleChanged", new SteamVR_Utils.Event.Handler(this.OnTrackedDeviceRoleChanged));
  53. }
  54. private void OnInputFocus(params object[] args)
  55. {
  56. bool flag = (bool)args[0];
  57. if (flag)
  58. {
  59. for (int i = 0; i < this.objects.Length; i++)
  60. {
  61. GameObject gameObject = this.objects[i];
  62. if (gameObject != null)
  63. {
  64. string str = (i >= 2) ? (i - 1).ToString() : SteamVR_ControllerManager.labels[i];
  65. this.ShowObject(gameObject.transform, "hidden (" + str + ")");
  66. }
  67. }
  68. }
  69. else
  70. {
  71. for (int j = 0; j < this.objects.Length; j++)
  72. {
  73. GameObject gameObject2 = this.objects[j];
  74. if (gameObject2 != null)
  75. {
  76. string str2 = (j >= 2) ? (j - 1).ToString() : SteamVR_ControllerManager.labels[j];
  77. this.HideObject(gameObject2.transform, "hidden (" + str2 + ")");
  78. }
  79. }
  80. }
  81. }
  82. private void HideObject(Transform t, string name)
  83. {
  84. Transform transform = new GameObject(name).transform;
  85. transform.parent = t.parent;
  86. t.parent = transform;
  87. transform.gameObject.SetActive(false);
  88. }
  89. private void ShowObject(Transform t, string name)
  90. {
  91. Transform parent = t.parent;
  92. if (parent.gameObject.name != name)
  93. {
  94. return;
  95. }
  96. t.parent = parent.parent;
  97. UnityEngine.Object.Destroy(parent.gameObject);
  98. }
  99. private void SetTrackedDeviceIndex(int objectIndex, uint trackedDeviceIndex)
  100. {
  101. if (trackedDeviceIndex != 4294967295U)
  102. {
  103. for (int i = 0; i < this.objects.Length; i++)
  104. {
  105. if (i != objectIndex && this.indices[i] == trackedDeviceIndex)
  106. {
  107. GameObject gameObject = this.objects[i];
  108. if (gameObject != null)
  109. {
  110. gameObject.SetActive(false);
  111. }
  112. this.indices[i] = uint.MaxValue;
  113. }
  114. }
  115. }
  116. if (trackedDeviceIndex != this.indices[objectIndex])
  117. {
  118. this.indices[objectIndex] = trackedDeviceIndex;
  119. GameObject gameObject2 = this.objects[objectIndex];
  120. if (gameObject2 != null)
  121. {
  122. if (trackedDeviceIndex == 4294967295U)
  123. {
  124. gameObject2.SetActive(false);
  125. }
  126. else
  127. {
  128. gameObject2.SetActive(true);
  129. gameObject2.BroadcastMessage("SetDeviceIndex", (int)trackedDeviceIndex, SendMessageOptions.DontRequireReceiver);
  130. }
  131. }
  132. }
  133. }
  134. private void OnTrackedDeviceRoleChanged(params object[] args)
  135. {
  136. this.Refresh();
  137. }
  138. private void OnDeviceConnected(params object[] args)
  139. {
  140. uint num = (uint)((int)args[0]);
  141. bool flag = this.connected[(int)((UIntPtr)num)];
  142. this.connected[(int)((UIntPtr)num)] = false;
  143. bool flag2 = (bool)args[1];
  144. if (flag2)
  145. {
  146. CVRSystem system = OpenVR.System;
  147. if (system != null && system.GetTrackedDeviceClass(num) == ETrackedDeviceClass.Controller)
  148. {
  149. this.connected[(int)((UIntPtr)num)] = true;
  150. flag = !flag;
  151. }
  152. }
  153. if (flag)
  154. {
  155. this.Refresh();
  156. }
  157. }
  158. public void Refresh()
  159. {
  160. int i = 0;
  161. CVRSystem system = OpenVR.System;
  162. if (system != null)
  163. {
  164. this.leftIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.LeftHand);
  165. this.rightIndex = system.GetTrackedDeviceIndexForControllerRole(ETrackedControllerRole.RightHand);
  166. }
  167. if (this.leftIndex == 4294967295U && this.rightIndex == 4294967295U)
  168. {
  169. uint num = 0U;
  170. while ((ulong)num < (ulong)((long)this.connected.Length))
  171. {
  172. if (this.connected[(int)((UIntPtr)num)])
  173. {
  174. this.SetTrackedDeviceIndex(i++, num);
  175. break;
  176. }
  177. num += 1U;
  178. }
  179. }
  180. else
  181. {
  182. this.SetTrackedDeviceIndex(i++, ((ulong)this.rightIndex >= (ulong)((long)this.connected.Length) || !this.connected[(int)((UIntPtr)this.rightIndex)]) ? uint.MaxValue : this.rightIndex);
  183. this.SetTrackedDeviceIndex(i++, ((ulong)this.leftIndex >= (ulong)((long)this.connected.Length) || !this.connected[(int)((UIntPtr)this.leftIndex)]) ? uint.MaxValue : this.leftIndex);
  184. if (this.leftIndex != 4294967295U && this.rightIndex != 4294967295U)
  185. {
  186. uint num2 = 0U;
  187. while ((ulong)num2 < (ulong)((long)this.connected.Length))
  188. {
  189. if (i >= this.objects.Length)
  190. {
  191. break;
  192. }
  193. if (this.connected[(int)((UIntPtr)num2)])
  194. {
  195. if (num2 != this.leftIndex && num2 != this.rightIndex)
  196. {
  197. this.SetTrackedDeviceIndex(i++, num2);
  198. }
  199. }
  200. num2 += 1U;
  201. }
  202. }
  203. }
  204. while (i < this.objects.Length)
  205. {
  206. this.SetTrackedDeviceIndex(i++, uint.MaxValue);
  207. }
  208. }
  209. public GameObject left;
  210. public GameObject right;
  211. public GameObject[] objects;
  212. private uint[] indices;
  213. private bool[] connected = new bool[16];
  214. private uint leftIndex = uint.MaxValue;
  215. private uint rightIndex = uint.MaxValue;
  216. private static string[] labels = new string[]
  217. {
  218. "left",
  219. "right"
  220. };
  221. }