using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace DebuggingEssentials { [DefaultExecutionOrder(-99999999)] public class WindowManager : MonoBehaviour { public class MouseCursor { public CursorLockMode lockState; public bool visible = true; public void GetCurrentState() { if (Cursor.lockState != CursorLockMode.None) { lockState = Cursor.lockState; } if (!Cursor.visible) { visible = Cursor.visible; } } public void RestoreState() { Cursor.lockState = lockState; Cursor.visible = visible; } public void SetActive() { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } } public class EventHandling { private FastList eventSystemList = new FastList(); public bool hasEntered; public void OnEnter() { if (!hasEntered) { hasEntered = true; EventSystem current = EventSystem.current; if (!(current == null) && current.enabled) { current.enabled = false; eventSystemList.Add(current); } } } public void OnExit() { if (hasEntered) { hasEntered = false; for (int i = 0; i < eventSystemList.Count; i++) { eventSystemList.items[i].enabled = true; } eventSystemList.Clear(); } } } public static WindowManager instance; public static EventHandling eventHandling = new EventHandling(); private static string tooltip = string.Empty; private static int lastWindowId; private static MouseCursor mouseCursor = new MouseCursor(); private static RenderTexture rtCanvas; private static RenderTexture rtOld; public SO_WindowManager windowData; public bool useDontDestroyOnLoadScene; public bool onlyUseViewToggleKeys; public bool useCanvas; public GameObject canvasGO; public RawImage rawImage; private RuntimeInspector runtimeInspector; private RuntimeConsole runtimeConsole; private Vector2 size; private int oldScreenWidth; private int oldScreenHeight; public static void CheckMouseCursorState() { mouseCursor.GetCurrentState(); if (RuntimeInspector.show || RuntimeConsole.show) { mouseCursor.SetActive(); } else { mouseCursor.RestoreState(); } } public static void ResetAllStatic() { RuntimeInspector.ResetStatic(); NavigationCamera.ResetStatic(); GUIChangeBool.ResetStatic(); DrawEnum.ResetStatic(); CullGroup.ResetStatic(); HtmlDebug.ResetStatic(); } private void Awake() { if (useDontDestroyOnLoadScene) { Object.DontDestroyOnLoad(base.gameObject); } ResetAllStatic(); instance = this; HtmlDebug.timeSinceStartup.Stop(); HtmlDebug.timeSinceStartup.Start(); UpdateUseCanvas(useCanvas); } private void Init() { if (!runtimeInspector) { runtimeInspector = GetComponentInChildren(includeInactive: true); } if (!runtimeConsole) { runtimeConsole = GetComponentInChildren(includeInactive: true); } RuntimeConsole.instance = runtimeConsole; if ((bool)runtimeInspector && !runtimeInspector.gameObject.activeInHierarchy) { SetActiveDeactive(runtimeInspector.gameObject); } if ((bool)runtimeConsole && !runtimeConsole.gameObject.activeInHierarchy) { SetActiveDeactive(runtimeConsole.gameObject); } if (!runtimeConsole && (bool)runtimeInspector) { RuntimeConsole.accessLevel = AccessLevel.Admin; } if ((bool)runtimeInspector || (bool)runtimeConsole) { RuntimeInspector.InitAssemblies(); } } private void InitCanvasRenderTexture() { if (rtCanvas == null || Screen.width != oldScreenWidth || Screen.height != oldScreenHeight) { GraphicsHelper.Dispose(ref rtCanvas); rtCanvas = new RenderTexture(Screen.width, Screen.height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear); rtCanvas.anisoLevel = 0; rtCanvas.filterMode = FilterMode.Point; rtCanvas.useMipMap = false; rtCanvas.Create(); rawImage.texture = rtCanvas; oldScreenWidth = Screen.width; oldScreenHeight = Screen.height; } } public void UpdateUseCanvas(bool useCanvas) { canvasGO.SetActive(useCanvas); if (!useCanvas) { GraphicsHelper.Dispose(ref rtCanvas); } } public static void BeginRenderTextureGUI(bool clear = false) { if (!instance.useCanvas || Event.current.type != EventType.Repaint) { return; } rtOld = RenderTexture.active; if (rtCanvas != null && RenderTexture.active != rtCanvas) { RenderTexture.active = rtCanvas; if (clear) { GL.Clear(clearDepth: false, clearColor: true, new Color(0f, 0f, 0f, 0f)); } } } public static void EndRenderTextureGUI() { if (instance.useCanvas && Event.current.type == EventType.Repaint) { RenderTexture.active = rtOld; } } private void OnEnable() { instance = this; Init(); } private void SetActiveDeactive(GameObject go) { go.SetActive(value: true); go.SetActive(value: false); } private void OnDestroy() { if (instance == this) { instance = null; } GraphicsHelper.Dispose(ref rtCanvas); } private void Update() { HtmlDebug.frameTime = (float)HtmlDebug.timeSinceStartup.ElapsedMilliseconds / 1000f; HtmlDebug.currentFrame = Time.frameCount; if ((bool)HtmlDebug.instance) { HtmlDebug.instance.UpdateLogs(); } if ((bool)RuntimeConsole.instance) { RuntimeConsole.instance.ManualUpdate(); } if ((bool)RuntimeInspector.instance) { RuntimeInspector.instance.ManualUpdate(); } } private void LateUpdate() { EventInput.ResetInput(); } private void OnGUI() { if (useCanvas) { InitCanvasRenderTexture(); } BeginRenderTextureGUI(clear: true); float value = windowData.guiScale.value; GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(value, value, value)); if (Event.current.type == EventType.Layout) { GUIChangeBool.ApplyUpdates(); } else { EventInput.GetInput(); } if ((bool)runtimeInspector) { runtimeInspector.MyOnGUI(); } if ((bool)runtimeConsole) { runtimeConsole.MyOnGUI(); } if (DoWindowsContainMousePos()) { eventHandling.OnEnter(); } else { eventHandling.OnExit(); } if ((bool)runtimeInspector && runtimeInspector.windowData.showTooltip) { DrawTooltip(EventInput.mousePos); } GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one); EndRenderTextureGUI(); } public static bool DoWindowsContainMousePos() { if ((bool)instance.runtimeInspector) { WindowSettings hierarchyWindow = instance.runtimeInspector.windowData.hierarchyWindow; WindowSettings inspectorWindow = instance.runtimeInspector.windowData.inspectorWindow; if (RuntimeInspector.show && (hierarchyWindow.ContainsMousePos(EventInput.mousePos) || (RuntimeInspector.ShouldInspectorWindowShow() && inspectorWindow.ContainsMousePos(EventInput.mousePos)))) { return true; } } if ((bool)instance.runtimeConsole) { WindowSettings consoleWindow = instance.runtimeConsole.windowData.consoleWindow; if (RuntimeConsole.show && consoleWindow.ContainsMousePos(EventInput.mousePos)) { return true; } } return false; } public static void SetToolTip(int windowId) { if (GUI.tooltip.Length > 0) { lastWindowId = windowId; tooltip = GUI.tooltip; } else if (windowId == lastWindowId) { tooltip = string.Empty; } } private void DrawTooltip(Vector2 mousePos) { if (tooltip.Length > 0) { size = GUI.skin.label.CalcSize(Helper.GetGUIContent(tooltip)); size.x += 10f; size.y += 2.5f; Rect clientRect = new Rect(mousePos.x, mousePos.y, size.x, size.y); clientRect.x += 5f; clientRect.y += 20f; clientRect = GUI.Window(423423, clientRect, DoWindow, GUIContent.none); } } private void DoWindow(int windowId) { GUI.BringWindowToFront(windowId); Rect position = new Rect(0f, 0f, size.x, size.y); GUI.backgroundColor = new Color(0.1f, 0.1f, 0.1f, 0.5f); GUI.Box(position, GUIContent.none); GUI.backgroundColor = Color.white; position.x += 5f; GUI.Label(position, tooltip); } } }