CursorManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.XR.CoreUtils;
  4. using UnityEngine;
  5. using UnityEngine.XR.Interaction.Toolkit;
  6. using Ximmerse.XR.InputSystems.GazeAndGestureInteraction;
  7. namespace Ximmerse.XR.InputSystems.GazeAndGestureInteraction
  8. {
  9. public class CursorManager : MonoBehaviour
  10. {
  11. #region Property
  12. private static CursorManager instance;
  13. public static CursorManager Instance
  14. {
  15. get
  16. {
  17. return instance;
  18. }
  19. }
  20. private GameObject cursorGo;
  21. private GameObject _reticle;
  22. private Sprite _normal;
  23. private Sprite _tracking;
  24. private Sprite _none;
  25. public GameObject CursorGo
  26. {
  27. get => cursorGo;
  28. }
  29. public Transform MainCamera
  30. {
  31. get => xROrigin.Camera.transform;
  32. }
  33. public GameObject Reticle
  34. {
  35. get => _reticle;
  36. }
  37. public Sprite Normal
  38. {
  39. get => _normal;
  40. set => _normal = value;
  41. }
  42. public Sprite Tracking
  43. {
  44. get => _tracking;
  45. set => _tracking = value;
  46. }
  47. public Sprite None
  48. {
  49. get => _none;
  50. set => _none = value;
  51. }
  52. private XROrigin xROrigin;
  53. private GameObject reticle;
  54. #endregion
  55. #region Unity
  56. private void Awake()
  57. {
  58. instance = this;
  59. }
  60. void Start()
  61. {
  62. xROrigin = FindObjectOfType<XROrigin>();
  63. if (GazeAndHandInteractionSystem.instance.normal == null)
  64. {
  65. _normal = Resources.Load<Sprite>("Gesture/Texture/Normal");
  66. }
  67. else
  68. {
  69. _normal = GazeAndHandInteractionSystem.instance.normal;
  70. }
  71. if (_tracking == null)
  72. {
  73. _tracking = Resources.Load<Sprite>("Gesture/Texture/Tracking");
  74. }
  75. else
  76. {
  77. _tracking = GazeAndHandInteractionSystem.instance.tracking;
  78. }
  79. if (_none == null)
  80. {
  81. _none = Resources.Load<Sprite>("Gesture/Texture/None");
  82. }
  83. else
  84. {
  85. _none = GazeAndHandInteractionSystem.instance.select;
  86. }
  87. cursorGo = Instantiate(Resources.Load("Gesture/Prefabs/Cursor"), xROrigin.Camera.transform) as GameObject;
  88. _reticle = Instantiate(Resources.Load("Gesture/Prefabs/CURSOR0 (2)")) as GameObject;
  89. //_normal = Resources.Load<Sprite>("Gesture/Texture/Normal");
  90. //_tracking = Resources.Load<Sprite>("Gesture/Texture/Tracking");
  91. //_none = Resources.Load<Sprite>("Gesture/Texture/None");
  92. reticle = new GameObject("reticle");
  93. reticle.transform.localScale = new Vector3(0.03f, 0.03f, 0.03f);
  94. _reticle.transform.parent = reticle.transform;
  95. reticle.AddComponent<CursorScale>();
  96. _reticle.transform.eulerAngles = new Vector3(90, 0, 0);
  97. _reticle.transform.localScale = new Vector3(1, 1, 1);
  98. _reticle.transform.localPosition = new Vector3(0, 0.5f, 0);
  99. GazeAndHandInteractionSystem.instance._eyeRay.GetComponent<XRInteractorLineVisual>().reticle = reticle;
  100. }
  101. private void Update()
  102. {
  103. cursorGo.SetActive(!reticle.activeSelf);
  104. }
  105. #endregion
  106. #region Method
  107. public void NormalCursor()
  108. {
  109. cursorGo.GetComponent<SpriteRenderer>().sprite = _normal;
  110. }
  111. public void HideCursor()
  112. {
  113. cursorGo.GetComponent<SpriteRenderer>().sprite = _none;
  114. }
  115. public void TrackingCursor()
  116. {
  117. cursorGo.GetComponent<SpriteRenderer>().sprite = _tracking;
  118. }
  119. public void NormalReticle()
  120. {
  121. _reticle.GetComponent<SpriteRenderer>().sprite = _normal;
  122. }
  123. public void TrackingReticle()
  124. {
  125. _reticle.GetComponent<SpriteRenderer>().sprite = _tracking;
  126. }
  127. public void HideReticle()
  128. {
  129. _reticle.GetComponent<SpriteRenderer>().sprite = _none;
  130. }
  131. #endregion
  132. }
  133. }