2021-04-25 00:21:12 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using System.Text;
|
2021-04-25 00:21:12 +10:00
|
|
|
|
using UnityEngine;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using UnityEngine.UI;
|
2021-05-09 20:18:33 +10:00
|
|
|
|
using UnityExplorer.UI;
|
2021-05-05 21:27:09 +10:00
|
|
|
|
using UnityExplorer.UI.CacheObject;
|
2021-05-09 20:18:33 +10:00
|
|
|
|
using UnityExplorer.UI.Inspectors;
|
2021-05-26 17:40:09 +10:00
|
|
|
|
using UnityExplorer.UI.Models;
|
2021-04-25 00:21:12 +10:00
|
|
|
|
using UnityExplorer.UI.Panels;
|
|
|
|
|
|
2021-05-09 20:18:33 +10:00
|
|
|
|
namespace UnityExplorer
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
|
|
|
|
public static class InspectorManager
|
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static readonly List<InspectorBase> Inspectors = new List<InspectorBase>();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static InspectorBase ActiveInspector { get; private set; }
|
2021-05-09 20:18:33 +10:00
|
|
|
|
private static InspectorBase lastActiveInspector;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
|
|
|
|
|
public static float PanelWidth;
|
|
|
|
|
|
2021-05-16 21:46:19 +10:00
|
|
|
|
internal static void CloseAllTabs()
|
|
|
|
|
{
|
|
|
|
|
if (Inspectors.Any())
|
|
|
|
|
{
|
|
|
|
|
for (int i = Inspectors.Count - 1; i >= 0; i--)
|
|
|
|
|
Inspectors[i].CloseInspector();
|
|
|
|
|
|
|
|
|
|
Inspectors.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 01:29:02 +10:00
|
|
|
|
public static void Inspect(object obj, CacheObjectBase sourceCache = null)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
if (obj.IsNullOrDestroyed())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
obj = obj.TryCast();
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
|
|
|
|
if (TryFocusActiveInspector(obj))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
if (obj is GameObject)
|
|
|
|
|
CreateInspector<GameObjectInspector>(obj);
|
2021-04-25 21:20:50 +10:00
|
|
|
|
else
|
2021-05-03 01:29:02 +10:00
|
|
|
|
CreateInspector<ReflectionInspector>(obj, false, sourceCache);
|
2021-04-25 21:20:50 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 23:58:13 +10:00
|
|
|
|
private static bool TryFocusActiveInspector(object target)
|
|
|
|
|
{
|
|
|
|
|
foreach (var inspector in Inspectors)
|
|
|
|
|
{
|
2021-05-03 01:29:02 +10:00
|
|
|
|
if (inspector.Target.ReferenceEqual(target))
|
2021-04-28 23:58:13 +10:00
|
|
|
|
{
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, true);
|
|
|
|
|
SetInspectorActive(inspector);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 20:47:48 +10:00
|
|
|
|
public static void Inspect(Type type)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
CreateInspector<ReflectionInspector>(type, true);
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static void SetInspectorActive(InspectorBase inspector)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
UnsetActiveInspector();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
ActiveInspector = inspector;
|
|
|
|
|
inspector.OnSetActive();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
public static void UnsetActiveInspector()
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
if (ActiveInspector != null)
|
2021-05-09 20:18:33 +10:00
|
|
|
|
{
|
|
|
|
|
lastActiveInspector = ActiveInspector;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
ActiveInspector.OnSetInactive();
|
2021-05-09 20:18:33 +10:00
|
|
|
|
ActiveInspector = null;
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
2021-05-16 21:46:19 +10:00
|
|
|
|
|
|
|
|
|
private static void CreateInspector<T>(object target, bool staticReflection = false,
|
|
|
|
|
CacheObjectBase sourceCache = null) where T : InspectorBase
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-26 19:56:21 +10:00
|
|
|
|
var inspector = Pool<T>.Borrow();
|
|
|
|
|
Inspectors.Add(inspector);
|
2021-05-03 01:29:02 +10:00
|
|
|
|
inspector.Target = target;
|
|
|
|
|
|
2021-05-09 02:22:03 +10:00
|
|
|
|
if (sourceCache != null && sourceCache.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 = sourceCache;
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, true);
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.UIRoot.transform.SetParent(InspectorPanel.Instance.ContentHolder.transform, false);
|
2021-04-25 00:21:12 +10:00
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
if (inspector is ReflectionInspector reflectInspector)
|
|
|
|
|
reflectInspector.StaticOnly = staticReflection;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.OnBorrowedFromPool(target);
|
|
|
|
|
SetInspectorActive(inspector);
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
internal static void ReleaseInspector<T>(T inspector) where T : InspectorBase
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-05-09 20:18:33 +10:00
|
|
|
|
if (lastActiveInspector == inspector)
|
|
|
|
|
lastActiveInspector = null;
|
|
|
|
|
|
|
|
|
|
bool wasActive = ActiveInspector == inspector;
|
|
|
|
|
int wasIdx = Inspectors.IndexOf(inspector);
|
|
|
|
|
|
|
|
|
|
Inspectors.Remove(inspector);
|
2021-04-26 19:56:21 +10:00
|
|
|
|
inspector.OnReturnToPool();
|
|
|
|
|
Pool<T>.Return(inspector);
|
|
|
|
|
|
2021-05-09 20:18:33 +10:00
|
|
|
|
if (wasActive)
|
|
|
|
|
{
|
|
|
|
|
ActiveInspector = null;
|
|
|
|
|
// Try focus another inspector, or close the window.
|
|
|
|
|
if (lastActiveInspector != null)
|
|
|
|
|
{
|
|
|
|
|
SetInspectorActive(lastActiveInspector);
|
|
|
|
|
lastActiveInspector = null;
|
|
|
|
|
}
|
|
|
|
|
else if (Inspectors.Any())
|
|
|
|
|
{
|
|
|
|
|
int newIdx = Math.Min(Inspectors.Count - 1, Math.Max(0, wasIdx - 1));
|
|
|
|
|
SetInspectorActive(Inspectors[newIdx]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UIManager.SetPanelActive(UIManager.Panels.Inspector, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
internal static void Update()
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
for (int i = Inspectors.Count - 1; i >= 0; i--)
|
|
|
|
|
Inspectors[i].Update();
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 21:22:48 +10:00
|
|
|
|
internal static void OnPanelResized(float width)
|
2021-04-25 00:21:12 +10:00
|
|
|
|
{
|
2021-04-27 21:22:48 +10:00
|
|
|
|
PanelWidth = width;
|
|
|
|
|
|
|
|
|
|
foreach (var obj in Inspectors)
|
|
|
|
|
{
|
|
|
|
|
if (obj is ReflectionInspector inspector)
|
|
|
|
|
{
|
|
|
|
|
inspector.SetLayouts();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 00:21:12 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|