Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 9b6f3fd

Browse files
committed
Cleanups / refactoring
1 parent bacac92 commit 9b6f3fd

File tree

4 files changed

+37
-38
lines changed

4 files changed

+37
-38
lines changed

src/ExplorerBehaviour.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,26 @@
99

1010
namespace UnityExplorer
1111
{
12-
// Handles all Behaviour update calls for UnityExplorer (Update, FixedUpdate, OnPostRender).
13-
// Basically just a wrapper which calls the corresponding methods in ExplorerCore.
14-
1512
public class ExplorerBehaviour : MonoBehaviour
1613
{
1714
internal static ExplorerBehaviour Instance { get; private set; }
1815

16+
#if CPP
17+
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
18+
#endif
19+
1920
internal static void Setup()
2021
{
2122
#if CPP
2223
ClassInjector.RegisterTypeInIl2Cpp<ExplorerBehaviour>();
2324
#endif
2425

25-
var obj = new GameObject("ExplorerBehaviour");
26-
GameObject.DontDestroyOnLoad(obj);
27-
obj.hideFlags |= HideFlags.HideAndDontSave;
26+
GameObject obj = new("ExplorerBehaviour");
27+
DontDestroyOnLoad(obj);
28+
obj.hideFlags = HideFlags.HideAndDontSave;
2829
Instance = obj.AddComponent<ExplorerBehaviour>();
2930
}
3031

31-
#if CPP
32-
public ExplorerBehaviour(IntPtr ptr) : base(ptr) { }
33-
#endif
34-
3532
internal void Update()
3633
{
3734
ExplorerCore.Update();

src/UI/Widgets/TransformTree/CachedTransform.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class CachedTransform
1010
{
1111
public TransformTree Tree { get; }
1212
public Transform Value { get; private set; }
13-
public int InstanceID { get; private set; }
13+
public int InstanceID { get; }
1414
public CachedTransform Parent { get; internal set; }
1515

1616
public int Depth { get; internal set; }
@@ -23,10 +23,11 @@ public class CachedTransform
2323

2424
public CachedTransform(TransformTree tree, Transform transform, int depth, CachedTransform parent = null)
2525
{
26+
InstanceID = transform.GetInstanceID();
27+
2628
Tree = tree;
2729
Value = transform;
2830
Parent = parent;
29-
InstanceID = transform.GetInstanceID();
3031
SiblingIndex = transform.GetSiblingIndex();
3132
Update(transform, depth);
3233
}

src/UI/Widgets/TransformTree/TransformCell.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class TransformCell : ICell
2929
public Action<GameObject> OnGameObjectClicked;
3030

3131
public CachedTransform cachedTransform;
32-
public int cellIndex;
3332

3433
public GameObject UIRoot { get; set; }
3534
public RectTransform Rect { get; set; }
@@ -53,7 +52,7 @@ public void Disable()
5352
UIRoot.SetActive(false);
5453
}
5554

56-
public void ConfigureCell(CachedTransform cached, int cellIndex)
55+
public void ConfigureCell(CachedTransform cached)
5756
{
5857
if (cached == null)
5958
{
@@ -64,7 +63,6 @@ public void ConfigureCell(CachedTransform cached, int cellIndex)
6463
if (!Enabled)
6564
Enable();
6665

67-
this.cellIndex = cellIndex;
6866
cachedTransform = cached;
6967

7068
spacer.minWidth = cached.Depth * 15;

src/UI/Widgets/TransformTree/TransformTree.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public class TransformTree : ICellPoolDataSource<TransformCell>
2222
// - Remove(object)
2323
// - set_Item[object]
2424
// These two methods have extremely bad performance due to using IndexOfKey(), which iterates the whole dictionary.
25-
// Currently we do not use either of these methods, so everything should be constant time hash lookups.
26-
// We DO make use of get_Item[object], get_Item[index], Add, Insert and RemoveAt, which OrderedDictionary perfectly meets our needs for.
25+
// Currently we do not use either of these methods, so everything should be constant time lookups.
26+
// We DO make use of get_Item[object], get_Item[index], Add, Insert, Contains and RemoveAt, which OrderedDictionary meets our needs for.
2727
/// <summary>
2828
/// Key: UnityEngine.Transform instance ID<br/>
2929
/// Value: CachedTransform
@@ -182,25 +182,25 @@ public void RefreshData(bool andRefreshUI, bool jumpToTop, bool stopExistingCoro
182182
traversedThisFrame.Reset();
183183
traversedThisFrame.Start();
184184

185-
IEnumerable<GameObject> rootObjects = GetRootEntriesMethod.Invoke();
186-
187-
refreshCoroutine = RuntimeHelper.StartCoroutine(RefreshCoroutine(rootObjects, andRefreshUI, jumpToTop, oneShot));
185+
refreshCoroutine = RuntimeHelper.StartCoroutine(RefreshCoroutine(andRefreshUI, jumpToTop, oneShot));
188186
}
189187

190-
// Coroutine for batched updates, max 2000 gameobjects per frame so FPS doesn't get tanked when there is like 100k gameobjects.
191-
// if "oneShot", then this will NOT be batched (if we need an immediate full update).
192-
IEnumerator RefreshCoroutine(IEnumerable<GameObject> rootObjects, bool andRefreshUI, bool jumpToTop, bool oneShot)
188+
IEnumerator RefreshCoroutine(bool andRefreshUI, bool jumpToTop, bool oneShot)
193189
{
190+
// Instead of doing string.IsNullOrEmpty(CurrentFilter) many times, let's just do it once per update.
191+
bool filtering = Filtering;
192+
193+
IEnumerable<GameObject> rootObjects = GetRootEntriesMethod();
194194
foreach (var gameObj in rootObjects)
195195
{
196-
if (gameObj)
196+
if (!gameObj)
197+
continue;
198+
199+
IEnumerator enumerator = Traverse(gameObj.transform, null, 0, oneShot, filtering);
200+
while (enumerator.MoveNext())
197201
{
198-
var enumerator = Traverse(gameObj.transform, null, 0, oneShot);
199-
while (enumerator.MoveNext())
200-
{
201-
if (!oneShot)
202-
yield return enumerator.Current;
203-
}
202+
if (!oneShot)
203+
yield return enumerator.Current;
204204
}
205205
}
206206

@@ -224,7 +224,7 @@ IEnumerator RefreshCoroutine(IEnumerable<GameObject> rootObjects, bool andRefres
224224

225225
// Recursive method to check a Transform and its children (if expanded).
226226
// Parent and depth can be null/default.
227-
private IEnumerator Traverse(Transform transform, CachedTransform parent, int depth, bool oneShot)
227+
private IEnumerator Traverse(Transform transform, CachedTransform parent, int depth, bool oneShot, bool filtering)
228228
{
229229
// Let's only tank 2ms of each frame (60->53fps)
230230
if (traversedThisFrame.ElapsedMilliseconds > 2)
@@ -236,21 +236,20 @@ private IEnumerator Traverse(Transform transform, CachedTransform parent, int de
236236

237237
int instanceID = transform.GetInstanceID();
238238

239+
// Unlikely, but since this method is async it could theoretically happen in extremely rare circumstances
239240
if (visited.Contains(instanceID))
240241
yield break;
241242

242-
if (Filtering)
243+
if (filtering)
243244
{
244245
if (!FilterHierarchy(transform))
245246
yield break;
246247

247-
visited.Add(instanceID);
248-
249248
if (!autoExpandedIDs.Contains(instanceID))
250249
autoExpandedIDs.Add(instanceID);
251250
}
252-
else
253-
visited.Add(instanceID);
251+
252+
visited.Add(instanceID);
254253

255254
CachedTransform cached;
256255
if (cachedTransforms.Contains(instanceID))
@@ -286,9 +285,11 @@ private IEnumerator Traverse(Transform transform, CachedTransform parent, int de
286285

287286
if (IsTransformExpanded(instanceID) && cached.Value.childCount > 0)
288287
{
288+
ExplorerCore.Log($"Traversing expanded transform {cached.Value.name} ({cached.InstanceID})");
289+
289290
for (int i = 0; i < transform.childCount; i++)
290291
{
291-
var enumerator = Traverse(transform.GetChild(i), cached, depth + 1, oneShot);
292+
var enumerator = Traverse(transform.GetChild(i), cached, depth + 1, oneShot, filtering);
292293
while (enumerator.MoveNext())
293294
{
294295
if (!oneShot)
@@ -317,7 +318,7 @@ public void SetCell(TransformCell cell, int index)
317318
{
318319
if (index < cachedTransforms.Count)
319320
{
320-
cell.ConfigureCell((CachedTransform)cachedTransforms[index], index);
321+
cell.ConfigureCell((CachedTransform)cachedTransforms[index]);
321322
if (Filtering)
322323
{
323324
if (cell.cachedTransform.Name.ContainsIgnoreCase(currentFilter))
@@ -345,6 +346,8 @@ private void OnGameObjectClicked(GameObject obj)
345346

346347
public void OnCellExpandToggled(CachedTransform cache)
347348
{
349+
ExplorerCore.Log($"OnCellExpandToggled: {cache.Value.name} ({cache.InstanceID})");
350+
348351
var instanceID = cache.InstanceID;
349352
if (expandedInstanceIDs.Contains(instanceID))
350353
expandedInstanceIDs.Remove(instanceID);

0 commit comments

Comments
 (0)