Skip to content

Commit ac7a74f

Browse files
committed
Applied pull request suggestions
1 parent d1b1878 commit ac7a74f

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

Assets/Editor Toolbox/Editor/Hierarchy/HierarchyPropertyLabel.cs

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34

45
using UnityEditor;
@@ -293,37 +294,26 @@ public override void OnGui(Rect rect)
293294
}
294295
}
295296

296-
private class HierarchyTreeLinesLabel : HierarchyPropertyLabel
297+
private class HierarchyTreeLinesLabel : HierarchyPropertyLabel, IDisposable
297298
{
298299
private const float firstElementWidthOffset = 4.0f;
299300
private const float firstElementXOffset = -45.0f;
300301
private const float startXPosition = 30.0f;
301302
private const float columnSize = 14.0f;
302303

303-
private List<TreeLineLevelRenderer> levelRenderers = new List<TreeLineLevelRenderer>();
304+
private readonly List<TreeLineLevelRenderer> levelRenderers = new List<TreeLineLevelRenderer>();
304305
private int itemRenderCount = 0;
305306

306-
public override sealed bool UsesWholeItemRect => true;
307-
308-
public override sealed bool DrawForHeaders => true;
309-
310-
private bool IsFirstRenderedElement => itemRenderCount == 0;
311-
312307
public HierarchyTreeLinesLabel()
313308
{
314-
EditorApplication.update += ResetItemRenderCount;
309+
EditorApplication.update += ResetItemRenderCount;
315310
}
316311

317-
~HierarchyTreeLinesLabel()
312+
public void Dispose()
318313
{
319314
EditorApplication.update -= ResetItemRenderCount;
320315
}
321316

322-
private void ResetItemRenderCount()
323-
{
324-
itemRenderCount = 0;
325-
}
326-
327317
public override sealed void OnGui(Rect rect)
328318
{
329319
if (Event.current.type != EventType.Repaint)
@@ -348,8 +338,8 @@ public override sealed void OnGui(Rect rect)
348338
rect.x = startXPosition;
349339
rect.width = columnSize + firstElementWidthOffset;
350340

351-
var transf = target.transform;
352-
var siblingIndex = transf.GetSiblingIndex();
341+
var targetTransform = target.transform;
342+
var siblingIndex = targetTransform.GetSiblingIndex();
353343

354344
if (levels > levelRenderers.Count)
355345
{
@@ -364,14 +354,15 @@ public override sealed void OnGui(Rect rect)
364354

365355
x--;
366356

367-
Transform transfBuf = transf;
357+
Transform transformBuffer = targetTransform;
368358
for (; x >= startIndex; x--)
369359
{
370-
levelRenderers[x].Initialize(transfBuf);
371-
transfBuf = transfBuf.parent;
360+
levelRenderers[x].Initialize(transformBuffer);
361+
transformBuffer = transformBuffer.parent;
372362
}
373363
}
374364

365+
Color colorCache = GUI.color;
375366
GUI.color = Color.gray;
376367

377368
int i = 0;
@@ -380,19 +371,31 @@ public override sealed void OnGui(Rect rect)
380371
levelRenderers[i].OnGUI(rect, target, siblingIndex, false);
381372
rect.x += columnSize;
382373
}
374+
383375
levelRenderers[i].OnGUI(rect, target, siblingIndex, true);
384376

385-
GUI.color = Color.white;
377+
GUI.color = colorCache;
386378
}
387379

380+
private void ResetItemRenderCount()
381+
{
382+
itemRenderCount = 0;
383+
}
384+
385+
public override sealed bool UsesWholeItemRect => true;
386+
387+
public override sealed bool DrawForHeaders => true;
388+
389+
private bool IsFirstRenderedElement => itemRenderCount == 0;
390+
388391
private class TreeLineLevelRenderer
389392
{
390393
private bool renderedLastLevelGameobject = false;
391394

392-
public void Initialize(Transform transf)
395+
public void Initialize(Transform transform)
393396
{
394-
var siblingIndex = transf.GetSiblingIndex();
395-
renderedLastLevelGameobject = GetParentChildCount(transf) == (siblingIndex + 1);
397+
var siblingIndex = transform.GetSiblingIndex();
398+
renderedLastLevelGameobject = GetParentChildCount(transform) == (siblingIndex + 1);
396399
}
397400

398401
public void OnGUI(Rect rect, GameObject target, int siblingIndex, bool isCurrentLevel)
@@ -419,27 +422,27 @@ public void OnGUI(Rect rect, GameObject target, int siblingIndex, bool isCurrent
419422
}
420423
}
421424

422-
private int GetParentChildCount(Transform target)
425+
private int GetParentChildCount(Transform transform)
423426
{
424-
var parent = target.parent;
427+
var parent = transform.parent;
425428
if (parent != null)
426429
{
427430
return parent.childCount;
428431
}
429432

430-
var scene = target.gameObject.scene;
433+
var scene = transform.gameObject.scene;
431434
return scene.rootCount;
432435
}
433436

434-
private int GetParentChildCount(GameObject target)
437+
private int GetParentChildCount(GameObject gameObject)
435438
{
436-
var parent = target.transform.parent;
439+
var parent = gameObject.transform.parent;
437440
if (parent != null)
438441
{
439442
return parent.childCount;
440443
}
441444

442-
var scene = target.scene;
445+
var scene = gameObject.scene;
443446
return scene.rootCount;
444447
}
445448
}

Assets/Editor Toolbox/Editor/ToolboxEditorHierarchy.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private static Rect AppendPropertyLabel(HierarchyPropertyLabel propertyLabel, Ga
186186
{
187187
if (propertyLabel.UsesWholeItemRect)
188188
{
189-
if(propertyLabel.Prepare(target, availableRect))
189+
if (propertyLabel.Prepare(target, availableRect))
190190
{
191191
propertyLabel.OnGui(availableRect);
192192
}
@@ -302,6 +302,14 @@ internal static void CreateAllowedHierarchyContentCallbacks(params HierarchyItem
302302

303303
internal static void RemoveAllowedHierarchyContentCallbacks()
304304
{
305+
for (int i = 0; i < propertyLabels.Count; i++)
306+
{
307+
if (propertyLabels[i] is IDisposable disposable)
308+
{
309+
disposable.Dispose();
310+
}
311+
}
312+
305313
propertyLabels.Clear();
306314
}
307315

0 commit comments

Comments
 (0)