mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-06-16 14:17:51 +08:00
Cleanups / refactoring
This commit is contained in:
parent
bacac929e9
commit
9b6f3fd3ea
@ -9,29 +9,26 @@ using UnhollowerRuntimeLib;
|
|||||||
|
|
||||||
namespace UnityExplorer
|
namespace UnityExplorer
|
||||||
{
|
{
|
||||||
// Handles all Behaviour update calls for UnityExplorer (Update, FixedUpdate, OnPostRender).
|
|
||||||
// Basically just a wrapper which calls the corresponding methods in ExplorerCore.
|
|
||||||
|
|
||||||
public class ExplorerBehaviour : MonoBehaviour
|
public class ExplorerBehaviour : MonoBehaviour
|
||||||
{
|
{
|
||||||
internal static ExplorerBehaviour Instance { get; private set; }
|
internal static ExplorerBehaviour Instance { get; private set; }
|
||||||
|
|
||||||
|
#if CPP
|
||||||
|
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
|
||||||
|
#endif
|
||||||
|
|
||||||
internal static void Setup()
|
internal static void Setup()
|
||||||
{
|
{
|
||||||
#if CPP
|
#if CPP
|
||||||
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
|
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
var obj = new GameObject("ExplorerBehaviour");
|
GameObject obj = new("ExplorerBehaviour");
|
||||||
GameObject.DontDestroyOnLoad(obj);
|
DontDestroyOnLoad(obj);
|
||||||
obj.hideFlags |= HideFlags.HideAndDontSave;
|
obj.hideFlags = HideFlags.HideAndDontSave;
|
||||||
Instance = obj.AddComponent<ExplorerBehaviour>();
|
Instance = obj.AddComponent<ExplorerBehaviour>();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CPP
|
|
||||||
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
internal void Update()
|
internal void Update()
|
||||||
{
|
{
|
||||||
ExplorerCore.Update();
|
ExplorerCore.Update();
|
||||||
|
@ -10,7 +10,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
{
|
{
|
||||||
public TransformTree Tree { get; }
|
public TransformTree Tree { get; }
|
||||||
public Transform Value { get; private set; }
|
public Transform Value { get; private set; }
|
||||||
public int InstanceID { get; private set; }
|
public int InstanceID { get; }
|
||||||
public CachedTransform Parent { get; internal set; }
|
public CachedTransform Parent { get; internal set; }
|
||||||
|
|
||||||
public int Depth { get; internal set; }
|
public int Depth { get; internal set; }
|
||||||
@ -23,10 +23,11 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
public CachedTransform(TransformTree tree, Transform transform, int depth, CachedTransform parent = null)
|
public CachedTransform(TransformTree tree, Transform transform, int depth, CachedTransform parent = null)
|
||||||
{
|
{
|
||||||
|
InstanceID = transform.GetInstanceID();
|
||||||
|
|
||||||
Tree = tree;
|
Tree = tree;
|
||||||
Value = transform;
|
Value = transform;
|
||||||
Parent = parent;
|
Parent = parent;
|
||||||
InstanceID = transform.GetInstanceID();
|
|
||||||
SiblingIndex = transform.GetSiblingIndex();
|
SiblingIndex = transform.GetSiblingIndex();
|
||||||
Update(transform, depth);
|
Update(transform, depth);
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
public Action<GameObject> OnGameObjectClicked;
|
public Action<GameObject> OnGameObjectClicked;
|
||||||
|
|
||||||
public CachedTransform cachedTransform;
|
public CachedTransform cachedTransform;
|
||||||
public int cellIndex;
|
|
||||||
|
|
||||||
public GameObject UIRoot { get; set; }
|
public GameObject UIRoot { get; set; }
|
||||||
public RectTransform Rect { get; set; }
|
public RectTransform Rect { get; set; }
|
||||||
@ -53,7 +52,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
UIRoot.SetActive(false);
|
UIRoot.SetActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ConfigureCell(CachedTransform cached, int cellIndex)
|
public void ConfigureCell(CachedTransform cached)
|
||||||
{
|
{
|
||||||
if (cached == null)
|
if (cached == null)
|
||||||
{
|
{
|
||||||
@ -64,7 +63,6 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
if (!Enabled)
|
if (!Enabled)
|
||||||
Enable();
|
Enable();
|
||||||
|
|
||||||
this.cellIndex = cellIndex;
|
|
||||||
cachedTransform = cached;
|
cachedTransform = cached;
|
||||||
|
|
||||||
spacer.minWidth = cached.Depth * 15;
|
spacer.minWidth = cached.Depth * 15;
|
||||||
|
@ -22,8 +22,8 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
// - Remove(object)
|
// - Remove(object)
|
||||||
// - set_Item[object]
|
// - set_Item[object]
|
||||||
// These two methods have extremely bad performance due to using IndexOfKey(), which iterates the whole dictionary.
|
// These two methods have extremely bad performance due to using IndexOfKey(), which iterates the whole dictionary.
|
||||||
// Currently we do not use either of these methods, so everything should be constant time hash lookups.
|
// Currently we do not use either of these methods, so everything should be constant time lookups.
|
||||||
// We DO make use of get_Item[object], get_Item[index], Add, Insert and RemoveAt, which OrderedDictionary perfectly meets our needs for.
|
// We DO make use of get_Item[object], get_Item[index], Add, Insert, Contains and RemoveAt, which OrderedDictionary meets our needs for.
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Key: UnityEngine.Transform instance ID<br/>
|
/// Key: UnityEngine.Transform instance ID<br/>
|
||||||
/// Value: CachedTransform
|
/// Value: CachedTransform
|
||||||
@ -182,25 +182,25 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
traversedThisFrame.Reset();
|
traversedThisFrame.Reset();
|
||||||
traversedThisFrame.Start();
|
traversedThisFrame.Start();
|
||||||
|
|
||||||
IEnumerable<GameObject> rootObjects = GetRootEntriesMethod.Invoke();
|
refreshCoroutine = RuntimeHelper.StartCoroutine(RefreshCoroutine(andRefreshUI, jumpToTop, oneShot));
|
||||||
|
|
||||||
refreshCoroutine = RuntimeHelper.StartCoroutine(RefreshCoroutine(rootObjects, andRefreshUI, jumpToTop, oneShot));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Coroutine for batched updates, max 2000 gameobjects per frame so FPS doesn't get tanked when there is like 100k gameobjects.
|
IEnumerator RefreshCoroutine(bool andRefreshUI, bool jumpToTop, bool oneShot)
|
||||||
// if "oneShot", then this will NOT be batched (if we need an immediate full update).
|
|
||||||
IEnumerator RefreshCoroutine(IEnumerable<GameObject> rootObjects, bool andRefreshUI, bool jumpToTop, bool oneShot)
|
|
||||||
{
|
{
|
||||||
|
// Instead of doing string.IsNullOrEmpty(CurrentFilter) many times, let's just do it once per update.
|
||||||
|
bool filtering = Filtering;
|
||||||
|
|
||||||
|
IEnumerable<GameObject> rootObjects = GetRootEntriesMethod();
|
||||||
foreach (var gameObj in rootObjects)
|
foreach (var gameObj in rootObjects)
|
||||||
{
|
{
|
||||||
if (gameObj)
|
if (!gameObj)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
IEnumerator enumerator = Traverse(gameObj.transform, null, 0, oneShot, filtering);
|
||||||
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
var enumerator = Traverse(gameObj.transform, null, 0, oneShot);
|
if (!oneShot)
|
||||||
while (enumerator.MoveNext())
|
yield return enumerator.Current;
|
||||||
{
|
|
||||||
if (!oneShot)
|
|
||||||
yield return enumerator.Current;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -224,7 +224,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
// Recursive method to check a Transform and its children (if expanded).
|
// Recursive method to check a Transform and its children (if expanded).
|
||||||
// Parent and depth can be null/default.
|
// Parent and depth can be null/default.
|
||||||
private IEnumerator Traverse(Transform transform, CachedTransform parent, int depth, bool oneShot)
|
private IEnumerator Traverse(Transform transform, CachedTransform parent, int depth, bool oneShot, bool filtering)
|
||||||
{
|
{
|
||||||
// Let's only tank 2ms of each frame (60->53fps)
|
// Let's only tank 2ms of each frame (60->53fps)
|
||||||
if (traversedThisFrame.ElapsedMilliseconds > 2)
|
if (traversedThisFrame.ElapsedMilliseconds > 2)
|
||||||
@ -236,21 +236,20 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
int instanceID = transform.GetInstanceID();
|
int instanceID = transform.GetInstanceID();
|
||||||
|
|
||||||
|
// Unlikely, but since this method is async it could theoretically happen in extremely rare circumstances
|
||||||
if (visited.Contains(instanceID))
|
if (visited.Contains(instanceID))
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
if (Filtering)
|
if (filtering)
|
||||||
{
|
{
|
||||||
if (!FilterHierarchy(transform))
|
if (!FilterHierarchy(transform))
|
||||||
yield break;
|
yield break;
|
||||||
|
|
||||||
visited.Add(instanceID);
|
|
||||||
|
|
||||||
if (!autoExpandedIDs.Contains(instanceID))
|
if (!autoExpandedIDs.Contains(instanceID))
|
||||||
autoExpandedIDs.Add(instanceID);
|
autoExpandedIDs.Add(instanceID);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
visited.Add(instanceID);
|
visited.Add(instanceID);
|
||||||
|
|
||||||
CachedTransform cached;
|
CachedTransform cached;
|
||||||
if (cachedTransforms.Contains(instanceID))
|
if (cachedTransforms.Contains(instanceID))
|
||||||
@ -286,9 +285,11 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
if (IsTransformExpanded(instanceID) && cached.Value.childCount > 0)
|
if (IsTransformExpanded(instanceID) && cached.Value.childCount > 0)
|
||||||
{
|
{
|
||||||
|
ExplorerCore.Log($"Traversing expanded transform {cached.Value.name} ({cached.InstanceID})");
|
||||||
|
|
||||||
for (int i = 0; i < transform.childCount; i++)
|
for (int i = 0; i < transform.childCount; i++)
|
||||||
{
|
{
|
||||||
var enumerator = Traverse(transform.GetChild(i), cached, depth + 1, oneShot);
|
var enumerator = Traverse(transform.GetChild(i), cached, depth + 1, oneShot, filtering);
|
||||||
while (enumerator.MoveNext())
|
while (enumerator.MoveNext())
|
||||||
{
|
{
|
||||||
if (!oneShot)
|
if (!oneShot)
|
||||||
@ -317,7 +318,7 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
{
|
{
|
||||||
if (index < cachedTransforms.Count)
|
if (index < cachedTransforms.Count)
|
||||||
{
|
{
|
||||||
cell.ConfigureCell((CachedTransform)cachedTransforms[index], index);
|
cell.ConfigureCell((CachedTransform)cachedTransforms[index]);
|
||||||
if (Filtering)
|
if (Filtering)
|
||||||
{
|
{
|
||||||
if (cell.cachedTransform.Name.ContainsIgnoreCase(currentFilter))
|
if (cell.cachedTransform.Name.ContainsIgnoreCase(currentFilter))
|
||||||
@ -345,6 +346,8 @@ namespace UnityExplorer.UI.Widgets
|
|||||||
|
|
||||||
public void OnCellExpandToggled(CachedTransform cache)
|
public void OnCellExpandToggled(CachedTransform cache)
|
||||||
{
|
{
|
||||||
|
ExplorerCore.Log($"OnCellExpandToggled: {cache.Value.name} ({cache.InstanceID})");
|
||||||
|
|
||||||
var instanceID = cache.InstanceID;
|
var instanceID = cache.InstanceID;
|
||||||
if (expandedInstanceIDs.Contains(instanceID))
|
if (expandedInstanceIDs.Contains(instanceID))
|
||||||
expandedInstanceIDs.Remove(instanceID);
|
expandedInstanceIDs.Remove(instanceID);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user