diff --git a/src/Inspectors/MouseInspector.cs b/src/Inspectors/MouseInspector.cs index c7c6e2d..6b5141d 100644 --- a/src/Inspectors/MouseInspector.cs +++ b/src/Inspectors/MouseInspector.cs @@ -46,9 +46,10 @@ namespace UnityExplorer.Inspectors public override bool CanDragAndResize => false; private Action inspectorAction = null; - internal Text objNameLabel; - internal Text objPathLabel; - internal Text mousePosLabel; + private Text inspectorLabelTitle; + private Text objNameLabel; + private Text objPathLabel; + private Text mousePosLabel; public MouseInspector(UIBase owner) : base(owner) { @@ -125,6 +126,43 @@ namespace UnityExplorer.Inspectors return Inspecting; } + /// + /// Updates the title text in the inspector UI, if the inspector title label is assigned. + /// + /// The new title text to display in the inspector. + internal void UpdateInspectorTitle(string title) + { + // Unity null check - if inspectorLabelTitle is assigned, update its text. + if (inspectorLabelTitle) + { + inspectorLabelTitle.text = title; + } + } + /// + /// Updates the object name label in the inspector UI, if the label is assigned. + /// + /// The new object name to display. + internal void UpdateObjectNameLabel(string name) + { + // Unity null check - if objNameLabel is assigned, update its text. + if (objNameLabel) + { + objNameLabel.text = name; + } + } + /// + /// Updates the object path label in the inspector UI, if the label is assigned. + /// + /// The new object path to display. + internal void UpdateObjectPathLabel(string path) + { + // Unity null check - if objPathLabel is assigned, update its text. + if (objPathLabel) + { + objPathLabel.text = path; + } + } + public void UpdateInspect() { if (IInputManager.GetKeyDown(KeyCode.Escape)) @@ -156,7 +194,7 @@ namespace UnityExplorer.Inspectors lastMousePos = mousePos; // use the raw mouse pos for the label - mousePosLabel.text = $"Mouse Position: {mousePos.ToString()}"; + mousePosLabel.text = $"Mouse Position: {mousePos.x}, {mousePos.y}"; // constrain the mouse pos we use within certain bounds if (mousePos.x < 350) @@ -196,11 +234,11 @@ namespace UnityExplorer.Inspectors // Title text - Text title = UIFactory.CreateLabel(inspectContent, + inspectorLabelTitle = UIFactory.CreateLabel(inspectContent, "InspectLabel", - "Mouse Inspector (press ESC to cancel)", + "", TextAnchor.MiddleCenter); - UIFactory.SetLayoutElement(title.gameObject, flexibleWidth: 9999); + UIFactory.SetLayoutElement(inspectorLabelTitle.gameObject, flexibleWidth: 9999); mousePosLabel = UIFactory.CreateLabel(inspectContent, "MousePosLabel", "Mouse Position:", TextAnchor.MiddleCenter); diff --git a/src/Inspectors/MouseInspectors/UiInspector.cs b/src/Inspectors/MouseInspectors/UiInspector.cs index c23cc20..a9aa377 100644 --- a/src/Inspectors/MouseInspectors/UiInspector.cs +++ b/src/Inspectors/MouseInspectors/UiInspector.cs @@ -17,10 +17,13 @@ namespace UnityExplorer.Inspectors.MouseInspectors private static readonly List wasDisabledCanvasGroups = new(); private static readonly List objectsAddedCastersTo = new(); + private const string DEFAULT_INSPECTOR_TITLE = "UI Inspector (press ESC to cancel)"; + public override void OnBeginMouseInspect() { SetupUIRaycast(); - MouseInspector.Instance.objPathLabel.text = ""; + MouseInspector.Instance.UpdateInspectorTitle(DEFAULT_INSPECTOR_TITLE); + MouseInspector.Instance.UpdateObjectPathLabel(""); } public override void ClearHitData() @@ -71,9 +74,9 @@ namespace UnityExplorer.Inspectors.MouseInspectors } if (currentHitObjects.Any()) - MouseInspector.Instance.objNameLabel.text = $"Click to view UI Objects under mouse: {currentHitObjects.Count}"; + MouseInspector.Instance.UpdateObjectNameLabel($"Click to view UI Objects under mouse: {currentHitObjects.Count}"); else - MouseInspector.Instance.objNameLabel.text = $"No UI objects under mouse."; + MouseInspector.Instance.UpdateObjectNameLabel( $"No UI objects under mouse."); } private static void SetupUIRaycast() diff --git a/src/Inspectors/MouseInspectors/WorldInspector.cs b/src/Inspectors/MouseInspectors/WorldInspector.cs index 85f4c01..90950d9 100644 --- a/src/Inspectors/MouseInspectors/WorldInspector.cs +++ b/src/Inspectors/MouseInspectors/WorldInspector.cs @@ -1,4 +1,6 @@ -namespace UnityExplorer.Inspectors.MouseInspectors +using UnityExplorer.UI.Panels; + +namespace UnityExplorer.Inspectors.MouseInspectors { public class WorldInspector : MouseInspectorBase { @@ -7,15 +9,26 @@ public override void OnBeginMouseInspect() { - MainCamera = Camera.main; - if (!MainCamera) + if (!EnsureMainCamera()) { ExplorerCore.LogWarning("No MainCamera found! Cannot inspect world!"); return; } } + /// + /// Assigns it as the MainCamera and updates the inspector title. + /// + /// The camera to assign. + private static void AssignCamAndUpdateTitle(Camera cam) + { + MainCamera = cam; + MouseInspector.Instance.UpdateInspectorTitle( + $"World Inspector ({MainCamera.name}) (press ESC to cancel)" + ); + } + public override void ClearHitData() { lastHitObject = null; @@ -26,11 +39,52 @@ inspectorAction(lastHitObject); } + /// + /// Attempts to ensure that MainCamera is assigned. If not then attempts to find it. + /// If no cameras are available, logs a warning and returns null. + /// + private static Camera EnsureMainCamera() + { + if (MainCamera){ + // We still call this in case the last title was from the UIInspector + AssignCamAndUpdateTitle(MainCamera); + return MainCamera; + } + + if (Camera.main){ + AssignCamAndUpdateTitle(Camera.main); + return MainCamera; + } + + ExplorerCore.LogWarning("No Camera.main found, trying to find a camera named 'Main Camera' or 'MainCamera'..."); + Camera namedCam = Camera.allCameras.FirstOrDefault(c => c.name is "Main Camera" or "MainCamera"); + if (namedCam) { + AssignCamAndUpdateTitle(namedCam); + return MainCamera; + } + + if (FreeCamPanel.inFreeCamMode && FreeCamPanel.GetFreecam()){ + AssignCamAndUpdateTitle(FreeCamPanel.GetFreecam()); + return MainCamera; + } + + ExplorerCore.LogWarning("No camera named found, using the first camera created..."); + var fallbackCam = Camera.allCameras.FirstOrDefault(); + if (fallbackCam) { + AssignCamAndUpdateTitle(fallbackCam); + return MainCamera; + } + + // If we reach here, no cameras were found at all. + ExplorerCore.LogWarning("No valid cameras found!"); + return null; + } + public override void UpdateMouseInspect(Vector2 mousePos) { - if (!MainCamera) - MainCamera = Camera.main; - if (!MainCamera) + // Attempt to ensure camera each time UpdateMouseInspect is called + // in case something changed or wasn't set initially. + if (!EnsureMainCamera()) { ExplorerCore.LogWarning("No Main Camera was found, unable to inspect world!"); MouseInspector.Instance.StopInspect(); @@ -51,8 +105,8 @@ if (obj != lastHitObject) { lastHitObject = obj; - MouseInspector.Instance.objNameLabel.text = $"Click to Inspect: {obj.name}"; - MouseInspector.Instance.objPathLabel.text = $"Path: {obj.transform.GetTransformPath(true)}"; + MouseInspector.Instance.UpdateObjectNameLabel($"Click to Inspect: {obj.name}"); + MouseInspector.Instance.UpdateObjectPathLabel($"Path: {obj.transform.GetTransformPath(true)}"); } }