暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MousePositionDebug.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
  2. #define USE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.Controls;
  5. #endif
  6. using UnityEditor;
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// Provides mouse position for debugging purpose.
  11. /// </summary>
  12. public class MousePositionDebug
  13. {
  14. // Singleton
  15. private static MousePositionDebug s_Instance = null;
  16. /// <summary>
  17. /// Singleton instance.
  18. /// </summary>
  19. static public MousePositionDebug instance
  20. {
  21. get
  22. {
  23. if (s_Instance == null)
  24. {
  25. s_Instance = new MousePositionDebug();
  26. }
  27. return s_Instance;
  28. }
  29. }
  30. #if UNITY_EDITOR
  31. [ExecuteAlways]
  32. class GameViewEventCatcher : MonoBehaviour
  33. {
  34. public static GameViewEventCatcher s_Instance = null;
  35. public static void Cleanup()
  36. {
  37. if (s_Instance != null)
  38. {
  39. // Either we call DestroyImmediate or Destroy we get an error :(
  40. // GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
  41. //DestroyImmediate(s_Instance.gameObject);
  42. //Destroy(s_Instance.gameObject);
  43. }
  44. }
  45. public static void Build()
  46. {
  47. Cleanup();
  48. var go = new GameObject("__GameViewEventCatcher");
  49. go.hideFlags = HideFlags.HideAndDontSave;
  50. s_Instance = go.AddComponent<GameViewEventCatcher>();
  51. }
  52. void Update()
  53. {
  54. Vector2 mousePosition;
  55. bool rightClickPressed = false;
  56. bool endKeyPressed = false;
  57. #if USE_INPUT_SYSTEM
  58. mousePosition = Pointer.current != null ? Pointer.current.position.ReadValue() : new Vector2(-1, -1);
  59. if (Mouse.current != null)
  60. rightClickPressed = Mouse.current.rightButton.isPressed;
  61. if (Keyboard.current != null)
  62. endKeyPressed = Keyboard.current.endKey.isPressed;
  63. #else
  64. mousePosition = Input.mousePosition;
  65. rightClickPressed = Input.GetMouseButton(1);
  66. endKeyPressed = Input.GetKey(KeyCode.End);
  67. #endif
  68. if (mousePosition.x < 0
  69. || mousePosition.y < 0
  70. || mousePosition.x > Screen.width
  71. || mousePosition.y > Screen.height)
  72. return;
  73. instance.m_mousePosition = mousePosition;
  74. instance.m_mousePosition.y = Screen.height - instance.m_mousePosition.y;
  75. if (rightClickPressed)
  76. instance.m_MouseClickPosition = instance.m_mousePosition;
  77. if (endKeyPressed)
  78. instance.m_MouseClickPosition = instance.m_mousePosition;
  79. }
  80. }
  81. private Vector2 m_mousePosition = Vector2.zero;
  82. Vector2 m_MouseClickPosition = Vector2.zero;
  83. private void OnSceneGUI(UnityEditor.SceneView sceneview)
  84. {
  85. m_mousePosition = Event.current.mousePosition;
  86. switch (Event.current.type)
  87. {
  88. case EventType.MouseDown:
  89. m_MouseClickPosition = m_mousePosition;
  90. break;
  91. case EventType.KeyDown:
  92. switch (Event.current.keyCode)
  93. {
  94. case KeyCode.End:
  95. // Usefull we you don't want to change the scene viewport but still update the mouse click position
  96. m_MouseClickPosition = m_mousePosition;
  97. sceneview.Repaint();
  98. break;
  99. }
  100. break;
  101. }
  102. }
  103. #endif
  104. /// <summary>
  105. /// Initialize the MousePositionDebug class.
  106. /// </summary>
  107. public void Build()
  108. {
  109. #if UNITY_EDITOR
  110. UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;
  111. UnityEditor.SceneView.duringSceneGui += OnSceneGUI;
  112. // Disabled as it cause error: GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
  113. //GameViewEventCatcher.Build();
  114. #endif
  115. }
  116. /// <summary>
  117. /// Cleanup the MousePositionDebug class.
  118. /// </summary>
  119. public void Cleanup()
  120. {
  121. #if UNITY_EDITOR
  122. UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;
  123. // Disabled as it cause error: GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
  124. //GameViewEventCatcher.Cleanup();
  125. #endif
  126. }
  127. /// <summary>
  128. /// Get the mouse position in the scene or game view.
  129. /// </summary>
  130. /// <param name="ScreenHeight">Window height.</param>
  131. /// <param name="sceneView">Get position in the scene view?</param>
  132. /// <returns>Coordinates of the mouse in the specified window.</returns>
  133. public Vector2 GetMousePosition(float ScreenHeight, bool sceneView)
  134. {
  135. #if UNITY_EDITOR
  136. if (sceneView)
  137. {
  138. // In play mode, m_mousePosition the one in the scene view
  139. Vector2 mousePixelCoord = EditorGUIUtility.PointsToPixels(m_mousePosition);
  140. mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
  141. return mousePixelCoord;
  142. }
  143. else
  144. {
  145. // In play mode, Input.mousecoords matches the position in the game view
  146. if (EditorApplication.isPlayingOrWillChangePlaymode)
  147. {
  148. return GetInputMousePosition();
  149. }
  150. else
  151. {
  152. // In non-play mode, only m_mousePosition is valid.
  153. // We force -1, -1 as a game view pixel pos to avoid
  154. // rendering un-wanted effects
  155. return new Vector2(-1.0f, -1.0f);
  156. }
  157. }
  158. #else
  159. // In app mode, we only use the Input.mousecoords
  160. return GetInputMousePosition();
  161. #endif
  162. }
  163. Vector2 GetInputMousePosition()
  164. {
  165. #if USE_INPUT_SYSTEM
  166. return Pointer.current != null ? Pointer.current.position.ReadValue() : new Vector2(-1, -1);
  167. #else
  168. return Input.mousePosition;
  169. #endif
  170. }
  171. /// <summary>
  172. /// Returns the position of the mouse click.
  173. /// </summary>
  174. /// <param name="ScreenHeight">Window height.</param>
  175. /// <returns>The coordinates of the mouse click.</returns>
  176. public Vector2 GetMouseClickPosition(float ScreenHeight)
  177. {
  178. #if UNITY_EDITOR
  179. Vector2 mousePixelCoord = EditorGUIUtility.PointsToPixels(m_MouseClickPosition);
  180. mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
  181. return mousePixelCoord;
  182. #else
  183. return Vector2.zero;
  184. #endif
  185. }
  186. }
  187. }