Skip to content

Commit 456c12f

Browse files
committed
Additional utility class responsible for caching scene-related data
1 parent f920bba commit 456c12f

5 files changed

Lines changed: 122 additions & 69 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Toolbox.Serialization
2+
{
3+
internal class SceneData
4+
{
5+
public string SceneName { get; set; }
6+
public string ScenePath { get; set; }
7+
public int BuildIndex { get; set; }
8+
}
9+
}

Assets/Editor Toolbox/Runtime/Serialization/SceneData.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.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Collections.Generic;
2+
#if UNITY_EDITOR
3+
using UnityEditor;
4+
#endif
5+
using UnityEngine;
6+
7+
namespace Toolbox.Serialization
8+
{
9+
internal static class SceneSerializationUtility
10+
{
11+
#if UNITY_EDITOR
12+
private readonly static Dictionary<SceneAsset, SceneData> cachedScenes = new Dictionary<SceneAsset, SceneData>();
13+
private static bool isInitialized;
14+
15+
16+
[InitializeOnLoadMethod]
17+
private static void Initialize()
18+
{
19+
if (isInitialized)
20+
{
21+
return;
22+
}
23+
24+
UnityEngine.Debug.Log("INIT");
25+
UpdateAllIndexes();
26+
27+
EditorBuildSettings.sceneListChanged -= UpdateAllIndexes;
28+
EditorBuildSettings.sceneListChanged += UpdateAllIndexes;
29+
isInitialized = true;
30+
}
31+
32+
private static void UpdateAllIndexes()
33+
{
34+
cachedScenes.Clear();
35+
UnityEngine.Debug.Log("UPDATE INDEXES");
36+
var buildIndex = -1;
37+
foreach (var scene in EditorBuildSettings.scenes)
38+
{
39+
if (!scene.enabled)
40+
{
41+
continue;
42+
}
43+
44+
buildIndex++;
45+
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path);
46+
if (sceneAsset != null)
47+
{
48+
cachedScenes.Add(sceneAsset, new SceneData()
49+
{
50+
BuildIndex = buildIndex,
51+
SceneName = sceneAsset.name,
52+
ScenePath = scene.path
53+
});
54+
}
55+
}
56+
}
57+
58+
59+
public static bool TryGetSceneData(SceneAsset sceneAsset, out SceneData data)
60+
{
61+
if (!sceneAsset || !cachedScenes.TryGetValue(sceneAsset, out data))
62+
{
63+
data = null;
64+
return false;
65+
}
66+
67+
return true;
68+
}
69+
#endif
70+
}
71+
}

Assets/Editor Toolbox/Runtime/Serialization/SceneSerializationUtility.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/SerializedScene.cs

Lines changed: 20 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
2+
using Toolbox.Serialization;
33
#if UNITY_EDITOR
44
using UnityEditor;
55
#endif
@@ -13,8 +13,6 @@ namespace UnityEngine
1313
public class SerializedScene : ISerializationCallbackReceiver
1414
{
1515
#if UNITY_EDITOR
16-
private readonly static Dictionary<SceneAsset, int> cachedBuildIndexes = new Dictionary<SceneAsset, int>();
17-
1816
[SerializeField]
1917
private SceneAsset sceneReference;
2018
#endif
@@ -26,87 +24,40 @@ public class SerializedScene : ISerializationCallbackReceiver
2624
private int buildIndex;
2725

2826

29-
void ISerializationCallbackReceiver.OnAfterDeserialize()
30-
{ }
31-
3227
void ISerializationCallbackReceiver.OnBeforeSerialize()
3328
{
34-
#if UNITY_EDITOR
35-
TryGetBuildIndex(sceneReference, out buildIndex);
36-
TryGetSceneName(sceneReference, out sceneName);
37-
TryGetScenePath(sceneReference, out scenePath);
38-
#endif
39-
}
40-
41-
//TODO:
42-
// 1 - move it to other class
43-
// 2 - try cache indexes in runtime but only if needed
44-
// 3 - update indexes before build
45-
#if UNITY_EDITOR
46-
[InitializeOnLoadMethod, RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
47-
private static void Initialize()
48-
{
49-
UpdateAllIndexes();
50-
51-
EditorBuildSettings.sceneListChanged -= UpdateAllIndexes;
52-
EditorBuildSettings.sceneListChanged += UpdateAllIndexes;
29+
UnityEngine.Debug.Log("BEFORE");
30+
UpdateProperties();
5331
}
5432

55-
private static void UpdateAllIndexes()
33+
void ISerializationCallbackReceiver.OnAfterDeserialize()
5634
{
57-
cachedBuildIndexes.Clear();
58-
59-
var buildIndex = -1;
60-
foreach (var scene in EditorBuildSettings.scenes)
61-
{
62-
if (scene.enabled)
63-
{
64-
buildIndex++;
65-
var sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path);
66-
if (sceneAsset != null)
67-
{
68-
cachedBuildIndexes.Add(sceneAsset, buildIndex);
69-
}
70-
}
71-
}
35+
UnityEngine.Debug.Log("AFTER");
36+
UpdateProperties();
7237
}
7338

74-
private static bool TryGetBuildIndex(SceneAsset sceneAsset, out int index)
75-
{
76-
if (sceneAsset == null || !cachedBuildIndexes.TryGetValue(sceneAsset, out index))
77-
{
78-
index = -1;
79-
return false;
80-
}
8139

82-
return true;
83-
}
84-
85-
private static bool TryGetSceneName(SceneAsset sceneAsset, out string name)
40+
private void UpdateProperties()
8641
{
87-
if (sceneAsset == null)
42+
#if UNITY_EDITOR
43+
if (SceneSerializationUtility.TryGetSceneData(sceneReference, out var sceneData))
8844
{
89-
name = null;
90-
return false;
45+
UnityEngine.Debug.Log("UPDATE");
46+
SceneName = sceneData.SceneName;
47+
ScenePath = sceneData.ScenePath;
48+
BuildIndex = sceneData.BuildIndex;
9149
}
92-
93-
name = sceneAsset.name;
94-
return true;
95-
}
96-
97-
public static bool TryGetScenePath(SceneAsset sceneAsset, out string path)
98-
{
99-
if (sceneAsset == null)
50+
else
10051
{
101-
path = null;
102-
return false;
52+
UnityEngine.Debug.Log("RESET");
53+
SceneName = string.Empty;
54+
ScenePath = string.Empty;
55+
BuildIndex = -1;
10356
}
104-
105-
path = AssetDatabase.GetAssetPath(sceneAsset);
106-
return true;
57+
#endif
10758
}
10859

109-
60+
#if UNITY_EDITOR
11061
public SceneAsset SceneReference
11162
{
11263
get => sceneReference;

0 commit comments

Comments
 (0)