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