-
Notifications
You must be signed in to change notification settings - Fork 869
Expand file tree
/
Copy pathDebugUpdater.cs
More file actions
214 lines (183 loc) · 7.59 KB
/
DebugUpdater.cs
File metadata and controls
214 lines (183 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.UI;
using UnityEngine.InputSystem.EnhancedTouch;
#endif
using System;
using System.Collections;
using System.Diagnostics;
using UnityEngine.EventSystems;
namespace UnityEngine.Rendering
{
[CoreRPHelpURL("Rendering-Debugger")]
class DebugUpdater : MonoBehaviour
{
static DebugUpdater s_Instance = null;
ScreenOrientation m_Orientation;
bool m_RuntimeUiWasVisibleLastFrame = false;
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void RuntimeInit()
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
if (DebugManager.instance.enableRuntimeUI)
EnableRuntime();
#endif
}
internal static void SetEnabled(bool enabled)
{
if (enabled)
EnableRuntime();
else
DisableRuntime();
}
static void EnableRuntime()
{
if (s_Instance != null)
return;
var go = new GameObject { name = "[Debug Updater]" };
s_Instance = go.AddComponent<DebugUpdater>();
s_Instance.m_Orientation = Screen.orientation;
DontDestroyOnLoad(go);
DebugManager.instance.EnableInputActions();
#if USE_INPUT_SYSTEM
EnhancedTouchSupport.Enable();
#endif
}
static void DisableRuntime()
{
DebugManager debugManager = DebugManager.instance;
debugManager.displayRuntimeUI = false;
debugManager.displayPersistentRuntimeUI = false;
if (s_Instance != null)
{
CoreUtils.Destroy(s_Instance.gameObject);
s_Instance = null;
}
}
internal static void HandleInternalEventSystemComponents(bool uiEnabled)
{
if (s_Instance == null)
return;
if (uiEnabled)
s_Instance.EnsureExactlyOneEventSystem();
else
s_Instance.DestroyDebugEventSystem();
}
void EnsureExactlyOneEventSystem()
{
var eventSystems = FindObjectsByType<EventSystem>();
var debugEventSystem = GetComponent<EventSystem>();
if (eventSystems.Length > 1 && debugEventSystem != null)
{
Debug.Log($"More than one EventSystem detected in scene. Destroying EventSystem owned by DebugUpdater.");
DestroyDebugEventSystem();
}
else if (eventSystems.Length == 0)
{
Debug.Log($"No EventSystem available. Creating a new EventSystem to enable Rendering Debugger runtime UI.");
CreateDebugEventSystem();
}
else
{
StartCoroutine(DoAfterInputModuleUpdated(CheckInputModuleExists));
}
}
IEnumerator DoAfterInputModuleUpdated(Action action)
{
// EventSystem.current.currentInputModule is not updated immediately when EventSystem.current changes. It happens
// with a delay in EventSystem.Update(), so wait a couple of frames to ensure that has happened.
yield return new WaitForEndOfFrame();
yield return new WaitForEndOfFrame();
action.Invoke();
}
void CheckInputModuleExists()
{
if (EventSystem.current != null && EventSystem.current.currentInputModule == null)
{
Debug.LogWarning("Found a game object with EventSystem component but no corresponding BaseInputModule component - Debug UI input might not work correctly.");
}
}
#if USE_INPUT_SYSTEM
void AssignDefaultActions()
{
if (EventSystem.current != null && EventSystem.current.currentInputModule is InputSystemUIInputModule inputSystemModule)
{
// FIXME: In order to activate default input actions in player builds (required for touch input to work),
// we need to call InputSystemUIInputModule.AssignDefaultActions() which was added in com.unity.inputsystem@1.1.0-pre.5.
// However, there is a problem in InputSystem package version ordering, where it sorts this version as an
// older version than it should be. Hence we cannot write a version define to conditionally compile this function call.
// Instead, we use reflection to see if the function is there and can be invoked.
//
// Once com.unity.inputsystem@1.1.0 is available, create an INPUTSYSTEM_1_1_0_OR_GREATER version define and use it
// to conditionally call AssignDefaultActions().
System.Reflection.MethodInfo assignDefaultActionsMethod = inputSystemModule.GetType().GetMethod("AssignDefaultActions");
if (assignDefaultActionsMethod != null)
{
assignDefaultActionsMethod.Invoke(inputSystemModule, null);
}
}
CheckInputModuleExists();
}
#endif
void CreateDebugEventSystem()
{
gameObject.AddComponent<EventSystem>();
#if USE_INPUT_SYSTEM
gameObject.AddComponent<InputSystemUIInputModule>();
StartCoroutine(DoAfterInputModuleUpdated(AssignDefaultActions));
#else
gameObject.AddComponent<StandaloneInputModule>();
#endif
}
void DestroyDebugEventSystem()
{
var eventSystem = GetComponent<EventSystem>();
#if USE_INPUT_SYSTEM
var inputModule = GetComponent<InputSystemUIInputModule>();
if (inputModule)
{
CoreUtils.Destroy(inputModule);
StartCoroutine(DoAfterInputModuleUpdated(AssignDefaultActions));
}
#else
CoreUtils.Destroy(GetComponent<StandaloneInputModule>());
CoreUtils.Destroy(GetComponent<BaseInput>());
#endif
CoreUtils.Destroy(eventSystem);
}
void Update()
{
DebugManager debugManager = DebugManager.instance;
// Runtime UI visibility can change i.e. due to scene unload - allow component cleanup in this case.
if (m_RuntimeUiWasVisibleLastFrame != debugManager.displayRuntimeUI)
{
HandleInternalEventSystemComponents(debugManager.displayRuntimeUI);
}
debugManager.UpdateActions();
if (debugManager.GetAction(DebugAction.EnableDebugMenu) != 0.0f ||
debugManager.GetActionToggleDebugMenuWithTouch())
{
debugManager.displayRuntimeUI = !debugManager.displayRuntimeUI;
}
if (debugManager.displayRuntimeUI)
{
if (debugManager.GetAction(DebugAction.ResetAll) != 0.0f)
debugManager.Reset();
if (debugManager.GetActionReleaseScrollTarget())
debugManager.SetScrollTarget(null); // Allow mouse wheel scroll without causing auto-scroll
}
if (m_Orientation != Screen.orientation)
{
StartCoroutine(RefreshRuntimeUINextFrame());
m_Orientation = Screen.orientation;
}
m_RuntimeUiWasVisibleLastFrame = debugManager.displayRuntimeUI;
}
static IEnumerator RefreshRuntimeUINextFrame()
{
yield return null; // Defer runtime UI refresh to next frame to allow canvas to update first.
DebugManager.instance.ReDrawOnScreenDebug();
}
}
}