MousePositionDebug.cs 6.1 KB

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