From cef8c12d2060c4b15f49025740d515123f75f34a Mon Sep 17 00:00:00 2001 From: Sinai <49360850+sinai-dev@users.noreply.github.com> Date: Fri, 22 Apr 2022 21:02:45 +1000 Subject: [PATCH] Fix TryFocusActiveInspector for classes --- src/Inspectors/InspectorBase.cs | 7 +++++- src/Inspectors/InspectorManager.cs | 34 +++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/Inspectors/InspectorBase.cs b/src/Inspectors/InspectorBase.cs index 9744595..2e1a3e5 100644 --- a/src/Inspectors/InspectorBase.cs +++ b/src/Inspectors/InspectorBase.cs @@ -1,6 +1,8 @@ -using UnityEngine; +using System; +using UnityEngine; using UnityEngine.UI; using UnityExplorer.UI.Panels; +using UniverseLib; using UniverseLib.UI.ObjectPool; namespace UnityExplorer.Inspectors @@ -9,6 +11,7 @@ namespace UnityExplorer.Inspectors { public bool IsActive { get; internal set; } public object Target { get; set; } + public Type TargetType { get; protected set; } public InspectorTab Tab { get; internal set; } @@ -24,6 +27,8 @@ namespace UnityExplorer.Inspectors public virtual void OnBorrowedFromPool(object target) { this.Target = target; + this.TargetType = target is Type type ? type : target.GetActualType(); + Tab = Pool.Borrow(); Tab.UIRoot.transform.SetParent(InspectorPanel.Instance.NavbarHolder.transform, false); diff --git a/src/Inspectors/InspectorManager.cs b/src/Inspectors/InspectorManager.cs index bd374d5..6e4ee42 100644 --- a/src/Inspectors/InspectorManager.cs +++ b/src/Inspectors/InspectorManager.cs @@ -23,7 +23,7 @@ namespace UnityExplorer public static event Action OnInspectedTabsChanged; - public static void Inspect(object obj, CacheObjectBase sourceCache = null) + public static void Inspect(object obj, CacheObjectBase parent = null) { if (obj.IsNullOrDestroyed()) return; @@ -36,19 +36,34 @@ namespace UnityExplorer if (obj is GameObject) CreateInspector(obj); else - CreateInspector(obj, false, sourceCache); + CreateInspector(obj, false, parent); } public static void Inspect(Type type) { + if (TryFocusActiveInspector(type)) + return; + CreateInspector(type, true); } - private static bool TryFocusActiveInspector(object target) + static bool TryFocusActiveInspector(object target) { foreach (InspectorBase inspector in Inspectors) { - if (inspector.Target.ReferenceEqual(target)) + bool shouldFocus = false; + + if (target is Type targetAsType) + { + if (inspector.TargetType.FullName == targetAsType.FullName) + shouldFocus = true; + } + else if(inspector.Target.ReferenceEqual(target)) + { + shouldFocus = true; + } + + if (shouldFocus) { UIManager.SetPanelActive(UIManager.Panels.Inspector, true); SetInspectorActive(inspector); @@ -76,7 +91,7 @@ namespace UnityExplorer } } - internal static void CloseAllTabs() + public static void CloseAllTabs() { if (Inspectors.Any()) { @@ -89,18 +104,17 @@ namespace UnityExplorer UIManager.SetPanelActive(UIManager.Panels.Inspector, false); } - private static void CreateInspector(object target, bool staticReflection = false, - CacheObjectBase parentObject = null) where T : InspectorBase + static void CreateInspector(object target, bool staticReflection = false, CacheObjectBase parent = null) where T : InspectorBase { T inspector = Pool.Borrow(); Inspectors.Add(inspector); inspector.Target = target; - if (parentObject != null && parentObject.CanWrite) + if (parent != null && parent.CanWrite) { // only set parent cache object if we are inspecting a struct, otherwise there is no point. if (target.GetType().IsValueType && inspector is ReflectionInspector ri) - ri.ParentCacheObject = parentObject; + ri.ParentCacheObject = parent; } UIManager.SetPanelActive(UIManager.Panels.Inspector, true); @@ -115,7 +129,7 @@ namespace UnityExplorer OnInspectedTabsChanged?.Invoke(); } - internal static void ReleaseInspector(T inspector) where T : InspectorBase + public static void ReleaseInspector(T inspector) where T : InspectorBase { if (lastActiveInspector == inspector) lastActiveInspector = null;