OSDN Git Service

Added Assets for main menu
[mindgames/Mindgames_main.git] / Mindgames / Library / PackageCache / com.unity.render-pipelines.high-definition@6.9.0-preview / Runtime / Core / Debugging / MousePositionDebug.cs
1 using System;
2 using UnityEditor;
3
4 namespace UnityEngine.Experimental.Rendering
5 {
6     public class MousePositionDebug
7     {
8         // Singleton
9         private static MousePositionDebug s_Instance = null;
10
11         static public MousePositionDebug instance
12         {
13             get
14             {
15                 if (s_Instance == null)
16                 {
17                     s_Instance = new MousePositionDebug();
18                 }
19
20                 return s_Instance;
21             }
22         }
23
24         public int debugStep
25         {
26             get
27             {
28 #if UNITY_EDITOR
29                 return m_DebugStep;
30 #else
31                 return 0;
32 #endif
33             }
34         }
35
36 #if UNITY_EDITOR
37         [ExecuteAlways]
38         class GameViewEventCatcher : MonoBehaviour
39         {
40             public static GameViewEventCatcher s_Instance = null;
41             public static void Cleanup()
42             {
43                 if (s_Instance != null)
44                 {
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);
49                 }
50             }
51
52             public static void Build()
53             {
54                 Cleanup();
55                 var go = new GameObject("__GameViewEventCatcher");
56                 go.hideFlags = HideFlags.HideAndDontSave;
57                 s_Instance = go.AddComponent<GameViewEventCatcher>();
58             }
59
60             void Update()
61             {
62                 if (Input.mousePosition.x < 0
63                     || Input.mousePosition.y < 0
64                     || Input.mousePosition.x > Screen.width
65                     || Input.mousePosition.y > Screen.height)
66                     return;
67
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;
78             }
79         }
80
81         private Vector2 m_mousePosition = Vector2.zero;
82         Vector2 m_MouseClickPosition = Vector2.zero;
83         int m_DebugStep = 0;
84
85         private void OnSceneGUI(UnityEditor.SceneView sceneview)
86         {
87             m_mousePosition = Event.current.mousePosition;
88             switch (Event.current.type)
89             {
90                 case EventType.MouseDown:
91                     m_MouseClickPosition = m_mousePosition;
92                     break;
93                 case EventType.KeyDown:
94                     switch (Event.current.keyCode)
95                     {
96                         case KeyCode.PageUp:
97                             ++m_DebugStep;
98                             sceneview.Repaint();
99                             break;
100                         case KeyCode.PageDown:
101                             m_DebugStep = Mathf.Max(0, m_DebugStep - 1);
102                             sceneview.Repaint();
103                             break;
104                         case KeyCode.End:
105                             // Usefull we you don't want to change the scene viewport but still update the mouse click position
106                             m_MouseClickPosition = m_mousePosition;
107                             sceneview.Repaint();
108                             break;
109                     }
110                     break;
111             }
112         }
113
114 #endif
115
116         public void Build()
117         {
118 #if UNITY_EDITOR
119 #if UNITY_2019_1_OR_NEWER
120             UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;
121             UnityEditor.SceneView.duringSceneGui += OnSceneGUI;
122 #else
123             UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
124             UnityEditor.SceneView.onSceneGUIDelegate += OnSceneGUI;
125 #endif
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         public void Cleanup()
132         {
133 #if UNITY_EDITOR
134 #if UNITY_2019_1_OR_NEWER
135             UnityEditor.SceneView.duringSceneGui -= OnSceneGUI;            
136 #else
137             UnityEditor.SceneView.onSceneGUIDelegate -= OnSceneGUI;
138 #endif
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();
141 #endif
142         }
143
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)
147         {
148 #if UNITY_EDITOR
149             if (sceneView)
150             {
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;
155             }
156             else
157             {
158                 // In play mode, Input.mousecoords matches the position in the game view
159                 if (EditorApplication.isPlayingOrWillChangePlaymode)
160                 {
161                     return Input.mousePosition;
162                 }
163                 else
164                 {
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);
169                 }
170             }
171 #else
172             // In app mode, we only use the Input.mousecoords
173             return Input.mousePosition;
174 #endif
175         }
176
177         public Vector2 GetMouseClickPosition(float ScreenHeight)
178         {
179 #if UNITY_EDITOR
180             Vector2 mousePixelCoord = m_MouseClickPosition;
181             mousePixelCoord.y = (ScreenHeight - 1.0f) - mousePixelCoord.y;
182             return mousePixelCoord;
183 #else
184             return Vector2.zero;
185 #endif
186         }
187     }
188 }