BorderState.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using SC.XR.Unity;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. public class BorderState : MonoBehaviour
  8. {
  9. CursorBase _cursorbase;
  10. MeshRenderer _mesh;
  11. GameObject _targetDeter;
  12. string _targetName;
  13. string _targetTempName;
  14. InputDevicePartBase _input;
  15. BorderSignState _signState;
  16. Color _blue ;
  17. Color _white;
  18. void ChangeState(BorderSignState state)
  19. {
  20. switch (state)
  21. {
  22. case BorderSignState.Normal:
  23. _mesh.material.color = _white;
  24. break;
  25. case BorderSignState.HighLight:
  26. _mesh.material.color = _blue;
  27. break;
  28. case BorderSignState.Selected:
  29. break;
  30. }
  31. }
  32. void InitBox()
  33. {
  34. _blue = new Color(0, 144, 255);
  35. _white = new Color(255, 255, 255);
  36. _mesh = gameObject.GetComponentInChildren<MeshRenderer>();
  37. _mesh.material.color = _white;
  38. _cursorbase = transform.parent.gameObject.GetComponent<DefaultCursor>();
  39. _input = _cursorbase.pointerBase.detectorBase.inputDevicePartBase;
  40. _targetTempName = "";
  41. }
  42. public bool isSuiableObj(GameObject target)
  43. {
  44. if (target != null )
  45. {
  46. if (!target.TryGetComponent(out BoundingBox box) && !target.TryGetComponent(out PressableButton btn))
  47. {
  48. if (target.transform.parent != null)
  49. {
  50. if (target.transform.parent.name== "BoundingBox")
  51. {
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. void ChangeMat()
  61. {
  62. if (isSuiableObj(_input.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject))
  63. {
  64. _signState = BorderSignState.HighLight;
  65. }
  66. else
  67. {
  68. _signState = BorderSignState.Normal;
  69. }
  70. ChangeState(_signState);
  71. }
  72. public void AddBorder(GameObject obj)
  73. {
  74. if (obj.GetComponent<BoundingBox>() == false)
  75. {
  76. obj.AddComponent<BoundingBox>();
  77. }
  78. if (obj.GetComponent<ManipulationHandler>() == false)
  79. {
  80. obj.AddComponent<ManipulationHandler>();
  81. }
  82. }
  83. public void DeterTarget(InputKeyCode inputKeyCode, InputDevicePartBase part)//松开确认
  84. {
  85. if (API_Module_AddBorder.IsEnableFunc())
  86. {
  87. if (inputKeyCode == InputKeyCode.Enter)
  88. {
  89. _targetDeter = part.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject;
  90. if (_targetDeter != null)
  91. {
  92. if (isSuiableObj(_targetDeter))
  93. {
  94. AddBorder(_targetDeter);
  95. _mesh.material.color = _white;
  96. }
  97. }
  98. else
  99. {
  100. API_Module_AddBorder.CloseFunc();
  101. Debug.Log("LGS:No object is currently detected");
  102. }
  103. }
  104. }
  105. }
  106. #region
  107. private void OnEnable()
  108. {
  109. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateRegister(DeterTarget);
  110. }
  111. private void Start()
  112. {
  113. InitBox();
  114. }
  115. private void Update()
  116. {
  117. //if (API_GSXR_Slam.SlamManager != null && !API_GSXR_Slam.GSXR_Is_SlamRunning())
  118. //{
  119. // return;
  120. //}
  121. if (_cursorbase != null)
  122. {
  123. if (_cursorbase.gameObject.activeSelf == false)
  124. {
  125. gameObject.SetActive(false);
  126. return;
  127. }
  128. if (_input.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject != null)
  129. {
  130. _targetName = _input.inputDataBase.SCPointEventData.pointerCurrentRaycast.gameObject.name;
  131. if (_targetTempName != _targetName)
  132. {
  133. _targetTempName = _targetName;
  134. ChangeMat();
  135. }
  136. }
  137. }
  138. }
  139. private void OnDisable()
  140. {
  141. }
  142. private void OnDestroy()
  143. {
  144. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateUnRegister(DeterTarget);
  145. _mesh = null;
  146. _cursorbase = null;
  147. _input = null;
  148. _targetDeter = null;
  149. }
  150. #endregion
  151. }