-
Notifications
You must be signed in to change notification settings - Fork 1
/
SceneLauncherEditor.cs
139 lines (115 loc) · 5.88 KB
/
SceneLauncherEditor.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
namespace HoloToolkit.Unity
{
[CustomEditor(typeof(SceneLauncher))]
public class SceneLauncherEditor : Editor
{
private SerializedProperty sceneMappingProperty;
private SerializedProperty buttonSpawnLocationProperty;
private SerializedProperty buttonPrefabProperty;
private SerializedProperty buttonRowMaxProperty;
private void OnEnable()
{
sceneMappingProperty = serializedObject.FindProperty("sceneMapping");
buttonSpawnLocationProperty = serializedObject.FindProperty("ButtonSpawnLocation");
buttonPrefabProperty = serializedObject.FindProperty("SceneButtonPrefab");
buttonRowMaxProperty = serializedObject.FindProperty("MaxRows");
CheckBuildScenes(sceneMappingProperty);
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(buttonSpawnLocationProperty);
EditorGUILayout.PropertyField(buttonPrefabProperty);
EditorGUILayout.IntSlider(buttonRowMaxProperty, 1, 10);
EditorGUILayout.HelpBox("To add scenes to the Scene Mapper by adding scenes in the build window.", MessageType.Info);
CheckBuildScenes(sceneMappingProperty);
ShowSceneList(sceneMappingProperty);
serializedObject.ApplyModifiedProperties();
}
private void ShowSceneList(SerializedProperty sceneList)
{
// property name with expansion widget
EditorGUILayout.PropertyField(sceneList);
if (EditorBuildSettings.scenes.Length == 0)
{
sceneList.ClearArray();
int index = sceneList.arraySize;
sceneList.InsertArrayElementAtIndex(index);
sceneList.GetArrayElementAtIndex(index).FindPropertyRelative("ScenePath").stringValue = SceneManager.GetActiveScene().path;
sceneList.GetArrayElementAtIndex(index).FindPropertyRelative("IsButtonEnabled").boolValue = true;
EditorBuildSettings.scenes = new[] { new EditorBuildSettingsScene(SceneManager.GetActiveScene().path, true) };
}
if (sceneList.isExpanded)
{
EditorGUI.indentLevel++;
// Scene rows
for (int i = 0; i < sceneList.arraySize; i++)
{
EditorGUILayout.BeginHorizontal();
// Disable the toggle if the scene is not enabled in the build settings.
GUI.enabled = EditorBuildSettings.scenes[i].enabled;
EditorGUILayout.PropertyField(sceneList.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled"));
GUI.enabled = false;
string path = sceneList.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue;
var sceneAsset = AssetDatabase.LoadAssetAtPath(path, typeof(SceneAsset)) as SceneAsset;
EditorGUILayout.ObjectField(sceneAsset, typeof(SceneAsset), false);
GUI.enabled = true;
EditorGUILayout.EndHorizontal();
}
EditorGUI.indentLevel--;
}
}
private void CheckBuildScenes(SerializedProperty list)
{
if (EditorBuildSettings.scenes.Length == 0)
{
return;
}
bool reBuildList = list.arraySize != EditorBuildSettings.scenes.Length;
if (!reBuildList)
{
// Check if the build settings list is the same as ours.
for (int i = 0; i < list.arraySize; i++)
{
if (list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue != EditorBuildSettings.scenes[i].path)
{
reBuildList = true;
}
}
}
if (reBuildList)
{
// if it's not then we need to store a copy of our mappings.
var oldBuildSceneMapping = new EditorBuildSettingsScene[list.arraySize];
for (int i = 0; i < list.arraySize; i++)
{
oldBuildSceneMapping[i] = new EditorBuildSettingsScene
{
path = list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue,
enabled = list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue
};
}
// Then re assign the mapping to the right scene in the build settings window.
list.ClearArray();
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
{
list.InsertArrayElementAtIndex(i);
list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue = EditorBuildSettings.scenes[i].path;
list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue = false;
for (var j = 0; j < oldBuildSceneMapping.Length; j++)
{
if (oldBuildSceneMapping[j].path == EditorBuildSettings.scenes[i].path)
{
list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue = oldBuildSceneMapping[j].enabled;
}
}
}
}
}
}
}