Skip to content

Commit fa24b37

Browse files
committed
Started grouping assetdatabase imports when stripping folders from prefabs
- Introduced the AssetImportGrouper class to make it easier to work with AssetDatabase.StartAssetEditing() - Wrapped prefab folder-stripping methods into AssetImportGrouper - Added AssetDatabase.Refresh at the end of RevertChanges to make the editor show changes instantly after exiting Play Mode.
1 parent 56d5116 commit fa24b37

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace UnityHierarchyFolders.Editor
2+
{
3+
using System;
4+
using UnityEditor;
5+
6+
internal class AssetImportGrouper : IDisposable
7+
{
8+
private static AssetImportGrouper _instance;
9+
10+
private AssetImportGrouper() { }
11+
12+
public static AssetImportGrouper Init()
13+
{
14+
AssetDatabase.StartAssetEditing();
15+
16+
if (_instance == null)
17+
_instance = new AssetImportGrouper();
18+
19+
return _instance;
20+
}
21+
22+
public void Dispose()
23+
{
24+
AssetDatabase.StopAssetEditing();
25+
}
26+
}
27+
}

Editor/Prefab Handling/AssetImportGrouper.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Prefab Handling/LabelHandler.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,15 @@ public class LabelHandler : AssetPostprocessor
1111

1212
private static void OnPostprocessAllAssets(string[] importedAssets, string[] _, string[] __, string[] ___)
1313
{
14-
try
14+
// Group imports into one to improve performance in case there are multiple prefabs that need a label change.
15+
using (AssetImportGrouper.Init())
1516
{
16-
// Group imports into one to improve performance in case there are multiple prefabs that need a label change.
17-
AssetDatabase.StartAssetEditing();
18-
1917
foreach (string assetPath in importedAssets)
2018
{
2119
if (assetPath.EndsWith(".prefab"))
2220
HandlePrefabLabels(assetPath);
2321
}
2422
}
25-
finally
26-
{
27-
AssetDatabase.StopAssetEditing();
28-
}
2923
}
3024

3125
private static void HandlePrefabLabels(string assetPath)

Editor/Prefab Handling/PrefabFolderStripper.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ static PrefabFolderStripper()
2222
public void OnPreprocessBuild(BuildReport report)
2323
{
2424
if (StripSettings.StripFoldersFromPrefabsInBuild)
25-
StripFoldersFromDependentPrefabs();
25+
{
26+
using (AssetImportGrouper.Init())
27+
{
28+
StripFoldersFromDependentPrefabs();
29+
}
30+
}
2631
}
2732

2833
public void OnPostprocessBuild(BuildReport report)
@@ -42,7 +47,10 @@ private static void HandlePrefabsOnPlayMode(PlayModeStateChange state)
4247
{
4348
// Stripping folders from all prefabs in the project instead of only the ones referenced in the scenes
4449
// because a prefab may be hot-swapped in Play Mode.
45-
StripFoldersFromAllPrefabs();
50+
using (AssetImportGrouper.Init())
51+
{
52+
StripFoldersFromAllPrefabs();
53+
}
4654
}
4755
else if (state == PlayModeStateChange.EnteredEditMode)
4856
{
@@ -124,6 +132,8 @@ private static void RevertChanges()
124132
{
125133
File.WriteAllText(path, content);
126134
}
135+
136+
AssetDatabase.Refresh();
127137
}
128138

129139
/// <summary>

0 commit comments

Comments
 (0)