Skip to content

Commit ad3a04e

Browse files
committed
Simplify Folder code
1 parent ae3c51e commit ad3a04e

File tree

1 file changed

+8
-31
lines changed

1 file changed

+8
-31
lines changed

Runtime/Folder.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ private Folder()
5858

5959
[SerializeField]
6060
private int _colorIndex = 0;
61-
62-
public int colorIndex => _colorIndex;
61+
public int ColorIndex => _colorIndex;
6362

6463
/// <summary>
6564
/// The set of folder objects.
@@ -85,31 +84,12 @@ public static bool TryGetIconIndex(UnityEngine.Object obj, out int index)
8584
/// <returns>Is this object a folder?</returns>
8685
public static bool IsFolder(UnityEngine.Object obj) => folders.ContainsKey(obj.GetInstanceID());
8786

88-
private void Start() => AddOrUpdateFolderData();
89-
private void OnValidate() => AddOrUpdateFolderData();
87+
private void Start() => AddFolderData();
88+
private void OnValidate() => AddFolderData();
9089
private void OnDestroy() => RemoveFolderData();
9190

92-
private void RemoveFolderData()
93-
{
94-
var instanceId = gameObject.GetInstanceID();
95-
if (folders.ContainsKey(instanceId))
96-
{
97-
folders.Remove(gameObject.GetInstanceID());
98-
}
99-
}
100-
101-
private void AddOrUpdateFolderData()
102-
{
103-
var instanceId = gameObject.GetInstanceID();
104-
if (folders.ContainsKey(instanceId))
105-
{
106-
folders[instanceId] = _colorIndex;
107-
}
108-
else
109-
{
110-
folders.Add(instanceId, _colorIndex);
111-
}
112-
}
91+
private void AddFolderData() => folders[gameObject.GetInstanceID()] = _colorIndex;
92+
private void RemoveFolderData() => folders.Remove(gameObject.GetInstanceID());
11393

11494
/// <summary>Hides all gizmos if selected to avoid accidental editing of the transform.</summary>
11595
private void HandleSelection()
@@ -158,10 +138,7 @@ private void EnsureExclusiveComponent()
158138
{
159139
// we are running, don't bother the player.
160140
// also, sometimes `this` might be null for whatever reason.
161-
if (Application.isPlaying || this == null)
162-
{
163-
return;
164-
}
141+
if (Application.isPlaying || this == null) { return; }
165142

166143
var existingComponents = this.GetComponents<Component>()
167144
.Where(c => c != this && !typeof(Transform).IsAssignableFrom(c.GetType()));
@@ -197,7 +174,7 @@ private void Update()
197174
#if UNITY_EDITOR
198175
if (!Application.IsPlaying(gameObject))
199176
{
200-
AddOrUpdateFolderData();
177+
AddFolderData();
201178
}
202179

203180
this.EnsureExclusiveComponent();
@@ -208,9 +185,9 @@ private void Update()
208185
public void Flatten()
209186
{
210187
// gather first-level children
188+
var index = transform.GetSiblingIndex(); // keep components in logical order
211189
foreach (Transform child in this.transform.GetComponentsInChildren<Transform>(includeInactive: true))
212190
{
213-
var index = transform.GetSiblingIndex();
214191
if (child.parent == this.transform)
215192
{
216193
child.name = $"{this.name}/{child.name}";

0 commit comments

Comments
 (0)