forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneList.cs
110 lines (102 loc) · 3.68 KB
/
SceneList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Text;
#if UNITY_EDITOR
using UnityEditor.Callbacks;
#endif
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Provides a list of names of scenes that were included in the Unity build settings.
/// This is necessary because SceneManager doesn't provide the names of scenes before they are loaded
/// and EditorBuildSettingsScene is only availabe in the editor.
/// </summary>
public class SceneList : Singleton<SceneList>
{
#pragma warning disable 0414, 0649 // sceneNamesUpdatedByBuild isn't assigned to, but it is set via Unity serialization
[SerializeField]
[HideInInspector]
private List<string> sceneNamesUpdatedByBuild;
#pragma warning restore 0414, 0649
protected override void Awake()
{
base.Awake();
LogSceneNames();
}
/// <summary>
/// Print the list of scene names to Debug.Log.
/// </summary>
private void LogSceneNames()
{
List<string> sceneNames = new List<string>(GetSceneNames());
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("SceneList has {0} scenes: ", sceneNames.Count);
for (int iScene = 0; iScene < sceneNames.Count; ++iScene)
{
string sceneName = sceneNames[iScene];
if (iScene > 0)
{
stringBuilder.Append(", ");
}
stringBuilder.Append(sceneName);
}
Debug.Log(stringBuilder.ToString());
}
/// <summary>
/// Get the list of scene names. Can be called either in the editor or from the running app.
/// </summary>
/// <returns>The list of scene names in build index order.</returns>
public List<string> GetSceneNames()
{
#if UNITY_EDITOR
// Use scene names from build settings directly from the editor.
return GetSceneNamesFromEditor();
#else
// Use the scene names serialized by OnPostProcessScene.
return sceneNamesUpdatedByBuild;
#endif
}
#if UNITY_EDITOR
/// <summary>
/// Get the list of scene names. Can only be called in the editor.
/// </summary>
/// <returns>The list of scene names in build index order.</returns>
private static List<string> GetSceneNamesFromEditor()
{
List<string> result = new List<string>();
foreach (UnityEditor.EditorBuildSettingsScene buildScene in UnityEditor.EditorBuildSettings.scenes)
{
if (!buildScene.enabled)
{
continue;
}
string name = Path.GetFileNameWithoutExtension(buildScene.path);
result.Add(name);
}
return result;
}
/// <summary>
/// Called by Unity when building the scene.
/// Saves the list of scene names in sceneNamesUpdatedByBuild.
/// </summary>
[PostProcessScene]
public static void OnPostProcessScene()
{
if (Application.isPlaying)
{
return;
}
SceneList sceneList = FindObjectOfType<SceneList>();
if (sceneList == null)
{
return;
}
sceneList.sceneNamesUpdatedByBuild = new List<string>(GetSceneNamesFromEditor());
sceneList.LogSceneNames();
}
#endif
}
}