4 namespace UnityEngine
.Experimental
.Rendering
6 public class MousePositionDebug
9 private static MousePositionDebug s_Instance
= null;
11 static public MousePositionDebug instance
15 if (s_Instance
== null)
17 s_Instance
= new MousePositionDebug();
38 class GameViewEventCatcher
: MonoBehaviour
40 public static GameViewEventCatcher s_Instance
= null;
41 public static void Cleanup()
43 if (s_Instance
!= null)
45 // Either we call DestroyImmediate or Destroy we get an error :(
46 // GameViewEventCatcher is only use for SSR debugging currently so comment this code and uncomment it if you want to debug SSR
47 //DestroyImmediate(s_Instance.gameObject);
48 //Destroy(s_Instance.gameObject);
52 public static void Build()
55 var go
= new GameObject("__GameViewEventCatcher");
56 go
.hideFlags
= HideFlags
.HideAndDontSave
;
57 s_Instance
= go
.AddComponent
<GameViewEventCatcher
>();
62 if (Input
.mousePosition
.x
< 0
63 || Input
.mousePosition
.y
< 0
64 || Input
.mousePosition
.x
> Screen
.width
65 || Input
.mousePosition
.y
> Screen
.height
)
68 instance
.m_mousePosition
= Input
.mousePosition
;
69 instance
.m_mousePosition
.y
= Screen
.height
- instance
.m_mousePosition
.y
;
70 if (Input
.GetMouseButton(1))
71 instance
.m_MouseClickPosition
= instance
.m_mousePosition
;
72 if (Input
.GetKey(KeyCode
.PageUp
))
73 ++instance
.m_DebugStep
;
74 if (Input
.GetKey(KeyCode
.PageDown
))
75 instance
.m_DebugStep
= Mathf
.Max(0, instance
.m_DebugStep
- 1);
76 if (Input
.GetKey(KeyCode
.End
))
77 instance
.m_MouseClickPosition
= instance
.m_mousePosition
;
81 private Vector2 m_mousePosition
= Vector2
.zero
;
82 Vector2 m_MouseClickPosition
= Vector2
.zero
;
85 private void OnSceneGUI(UnityEditor
.SceneView sceneview
)
87 m_mousePosition
= Event
.current
.mousePosition
;
88 switch (Event
.current
.type
)
90 case EventType
.MouseDown
:
91 m_MouseClickPosition
= m_mousePosition
;
93 case EventType
.KeyDown
:
94 switch (Event
.current
.keyCode
)
100 case KeyCode
.PageDown
:
101 m_DebugStep
= Mathf
.Max(0, m_DebugStep
- 1);
105 // Usefull we you don't want to change the scene viewport but still update the mouse click position
106 m_MouseClickPosition
= m_mousePosition
;
119 #if UNITY_2019_1_OR_NEWER
120 UnityEditor
.SceneView
.duringSceneGui
-= OnSceneGUI
;
121 UnityEditor
.SceneView
.duringSceneGui
+= OnSceneGUI
;
123 UnityEditor
.SceneView
.onSceneGUIDelegate
-= OnSceneGUI
;
124 UnityEditor
.SceneView
.onSceneGUIDelegate
+= OnSceneGUI
;
126 // 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
127 //GameViewEventCatcher.Build();
131 public void Cleanup()
134 #if UNITY_2019_1_OR_NEWER
135 UnityEditor
.SceneView
.duringSceneGui
-= OnSceneGUI
;
137 UnityEditor
.SceneView
.onSceneGUIDelegate
-= OnSceneGUI
;
139 // 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
140 //GameViewEventCatcher.Cleanup();
144 // This function can either return the mouse position in the scene view
145 // or in the game/game view.
146 public Vector2
GetMousePosition(float ScreenHeight
, bool sceneView
)
151 // In play mode, m_mousePosition the one in the scene view
152 Vector2 mousePixelCoord
= m_mousePosition
;
153 mousePixelCoord
.y
= (ScreenHeight
- 1.0f
) - mousePixelCoord
.y
;
154 return mousePixelCoord
;
158 // In play mode, Input.mousecoords matches the position in the game view
159 if (EditorApplication
.isPlayingOrWillChangePlaymode
)
161 return Input
.mousePosition
;
165 // In non-play mode, only m_mousePosition is valid.
166 // We force -1, -1 as a game view pixel pos to avoid
167 // rendering un-wanted effects
168 return new Vector2(-1.0f
, -1.0f
);
172 // In app mode, we only use the Input.mousecoords
173 return Input
.mousePosition
;
177 public Vector2
GetMouseClickPosition(float ScreenHeight
)
180 Vector2 mousePixelCoord
= m_MouseClickPosition
;
181 mousePixelCoord
.y
= (ScreenHeight
- 1.0f
) - mousePixelCoord
.y
;
182 return mousePixelCoord
;