ControllerManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using SC.XR.Unity;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using SC.XR.Unity.Module_InputSystem.InputDeviceGC;
  4. using SC.XR.Unity.Module_InputSystem.InputDeviceGC.KS;
  5. using TMPro;
  6. using UnityEngine;
  7. using UnityEngine.UI;
  8. using static API_GSXR_Module_InputSystem_KS;
  9. using static API_GSXR_Slam;
  10. public class ControllerManager : MonoBehaviour
  11. {
  12. string L_controllerType;
  13. string R_controllerType;
  14. public Transform leftController;
  15. public Transform rightController;
  16. public GameObject leftImg;
  17. public GameObject rightImg;
  18. protected bool reset_state;
  19. Transform R_Controller;
  20. Transform L_Controller;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. InputDeviceGCPartEventBase.eventDelegate += ControllerEvent;
  25. reset_state = true;
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. }
  31. void ControllerEvent(GCEventType aEvent, InputDeviceGCPart GCPart)
  32. {
  33. if (GCPart.PartType == InputDevicePartType.KSLeft)
  34. {
  35. if (aEvent == GCEventType.Connect)
  36. {
  37. L_controllerType = GCPart.inputDataGC.GCName;
  38. if (L_controllerType == "Nolo")
  39. {
  40. InitControllerPanel("NoloController",0);
  41. }
  42. else if (L_controllerType == "K102")
  43. {
  44. InitControllerPanel("K102Controller", 0);
  45. }
  46. else if (L_controllerType == "Luci")
  47. {
  48. InitControllerPanel("K102Controller", 0);
  49. }
  50. }
  51. else if (aEvent == GCEventType.DisConnect)
  52. {
  53. L_controllerType = "null";
  54. foreach (Transform child in leftController)
  55. {
  56. Destroy(child.gameObject);
  57. }
  58. leftImg.SetActive(false);
  59. }
  60. }
  61. if (GCPart.PartType == InputDevicePartType.KSRight)
  62. {
  63. if (aEvent == GCEventType.Connect)
  64. {
  65. R_controllerType = GCPart.inputDataGC.GCName;
  66. if (R_controllerType == "Nolo")
  67. {
  68. InitControllerPanel("NoloController", 1);
  69. }
  70. else if (R_controllerType == "K102")
  71. {
  72. InitControllerPanel("K102Controller", 1);
  73. }
  74. else if (R_controllerType == "Luci")
  75. {
  76. InitControllerPanel("K102Controller", 1);
  77. }
  78. }
  79. else if (aEvent == GCEventType.DisConnect)
  80. {
  81. R_controllerType = "null";
  82. foreach (Transform child in rightController)
  83. {
  84. Destroy(child.gameObject);
  85. }
  86. rightImg.SetActive(false);
  87. }
  88. }
  89. }
  90. protected void InitControllerPanel(string prefabName,int index)
  91. {
  92. if (index==1)
  93. {
  94. if (rightController.GetComponentsInChildren<ControllerInfoBase>().Length >=1)
  95. {
  96. return;
  97. }
  98. R_Controller = Instantiate(Resources.Load<GameObject>("prefabs/"+ prefabName)).transform;
  99. R_Controller.transform.parent = rightController;
  100. R_Controller.transform.localPosition = Vector3.zero;
  101. R_Controller.transform.localEulerAngles = Vector3.zero;
  102. R_Controller.transform.localScale = Vector3.one;
  103. rightImg.SetActive(true);
  104. R_Controller.GetComponent<ControllerInfoBase>().is_Left = false;
  105. }
  106. else if (index == 0)
  107. {
  108. if (leftController.GetComponentsInChildren<ControllerInfoBase>().Length >= 1)
  109. {
  110. return;
  111. }
  112. L_Controller = Instantiate(Resources.Load<GameObject>("prefabs/" + prefabName)).transform;
  113. L_Controller.parent = leftController;
  114. L_Controller.localPosition = Vector3.zero;
  115. L_Controller.localEulerAngles = Vector3.zero;
  116. L_Controller.localScale = Vector3.one;
  117. leftImg.SetActive(true);
  118. L_Controller.GetComponent<ControllerInfoBase>().is_Left = true;
  119. }
  120. }
  121. private void OnDestroy()
  122. {
  123. InputDeviceGCPartEventBase.eventDelegate -= ControllerEvent;
  124. }
  125. }