Skip to content

Commit 4ae3c1e

Browse files
committed
Handle removing and reimporting Scene assets
1 parent c3aff33 commit 4ae3c1e

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.IO;
2+
#if UNITY_EDITOR
3+
using UnityEditor;
4+
#endif
5+
6+
namespace Toolbox.Serialization
7+
{
8+
#if UNITY_EDITOR
9+
public class SceneSerializationProcessor : AssetPostprocessor
10+
{
11+
#if UNITY_2021_3_OR_NEWER
12+
internal static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
13+
#else
14+
internal static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
15+
#endif
16+
{
17+
foreach (string path in importedAssets)
18+
{
19+
if (IsSceneAsset(path))
20+
{
21+
SceneSerializationUtility.RefreshCache();
22+
return;
23+
}
24+
}
25+
26+
foreach (string path in deletedAssets)
27+
{
28+
if (IsSceneAsset(path))
29+
{
30+
SceneSerializationUtility.RefreshCache();
31+
return;
32+
}
33+
}
34+
}
35+
36+
private static bool IsSceneAsset(string path)
37+
{
38+
return Path.GetExtension(path) == ".unity";
39+
}
40+
}
41+
#endif
42+
}

Assets/Editor Toolbox/Runtime/Serialization/SceneSerializationProcessor.cs.meta

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

Assets/Editor Toolbox/Runtime/Serialization/SceneSerializationUtility.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ private static void Initialize()
2727
isInitialized = true;
2828
}
2929

30-
private static void ConfirmCache()
30+
internal static void ConfirmCache()
3131
{
3232
//NOTE: refresh data only if the cache is empty,
3333
//it probably means that it's our first time when we are updating it
@@ -37,7 +37,7 @@ private static void ConfirmCache()
3737
}
3838
}
3939

40-
private static void RefreshCache()
40+
internal static void RefreshCache()
4141
{
4242
cachedScenes.Clear();
4343
var buildIndex = -1;
@@ -48,23 +48,30 @@ private static void RefreshCache()
4848
continue;
4949
}
5050

51-
buildIndex++;
52-
var sceneIndex = scene.enabled ? buildIndex : InvalidSceneIndex;
5351
var sceneAsset = EditorGUIUtility.Load(scene.path) as SceneAsset;
54-
if (sceneAsset != null)
52+
if (sceneAsset == null)
5553
{
56-
if (cachedScenes.ContainsKey(sceneAsset))
57-
{
58-
continue;
59-
}
60-
61-
cachedScenes.Add(sceneAsset, new SceneData()
62-
{
63-
BuildIndex = buildIndex,
64-
SceneName = sceneAsset.name,
65-
ScenePath = scene.path
66-
});
54+
continue;
55+
}
56+
57+
var sceneIndex = InvalidSceneIndex;
58+
if (scene.enabled)
59+
{
60+
buildIndex++;
61+
sceneIndex = buildIndex;
62+
}
63+
64+
if (cachedScenes.ContainsKey(sceneAsset))
65+
{
66+
continue;
6767
}
68+
69+
cachedScenes.Add(sceneAsset, new SceneData()
70+
{
71+
BuildIndex = sceneIndex,
72+
SceneName = sceneAsset.name,
73+
ScenePath = scene.path
74+
});
6875
}
6976
}
7077

0 commit comments

Comments
 (0)