AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SceneLauncherEditor.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 using UnityEditor;
6 using UnityEngine.SceneManagement;
7 
8 namespace HoloToolkit.Unity
9 {
10  [CustomEditor(typeof(SceneLauncher))]
11  public class SceneLauncherEditor : Editor
12  {
13  private SerializedProperty sceneMappingProperty;
14  private SerializedProperty buttonSpawnLocationProperty;
15  private SerializedProperty buttonPrefabProperty;
16  private SerializedProperty buttonRowMaxProperty;
17 
18  private void OnEnable()
19  {
20  sceneMappingProperty = serializedObject.FindProperty("sceneMapping");
21  buttonSpawnLocationProperty = serializedObject.FindProperty("ButtonSpawnLocation");
22  buttonPrefabProperty = serializedObject.FindProperty("SceneButtonPrefab");
23  buttonRowMaxProperty = serializedObject.FindProperty("MaxRows");
24 
25  CheckBuildScenes(sceneMappingProperty);
26  }
27 
28  public override void OnInspectorGUI()
29  {
30  serializedObject.Update();
31  EditorGUILayout.PropertyField(buttonSpawnLocationProperty);
32  EditorGUILayout.PropertyField(buttonPrefabProperty);
33  EditorGUILayout.IntSlider(buttonRowMaxProperty, 1, 10);
34  EditorGUILayout.HelpBox("To add scenes to the Scene Mapper by adding scenes in the build window.", MessageType.Info);
35  CheckBuildScenes(sceneMappingProperty);
36  ShowSceneList(sceneMappingProperty);
37  serializedObject.ApplyModifiedProperties();
38  }
39 
40  private void ShowSceneList(SerializedProperty sceneList)
41  {
42  // property name with expansion widget
43  EditorGUILayout.PropertyField(sceneList);
44 
45  if (EditorBuildSettings.scenes.Length == 0)
46  {
47  sceneList.ClearArray();
48 
49  int index = sceneList.arraySize;
50 
51  sceneList.InsertArrayElementAtIndex(index);
52  sceneList.GetArrayElementAtIndex(index).FindPropertyRelative("ScenePath").stringValue = SceneManager.GetActiveScene().path;
53  sceneList.GetArrayElementAtIndex(index).FindPropertyRelative("IsButtonEnabled").boolValue = true;
54 
55  EditorBuildSettings.scenes = new[] { new EditorBuildSettingsScene(SceneManager.GetActiveScene().path, true) };
56  }
57 
58  if (sceneList.isExpanded)
59  {
60  EditorGUI.indentLevel++;
61 
62  // Scene rows
63  for (int i = 0; i < sceneList.arraySize; i++)
64  {
65  EditorGUILayout.BeginHorizontal();
66  // Disable the toggle if the scene is not enabled in the build settings.
67  GUI.enabled = EditorBuildSettings.scenes[i].enabled;
68 
69  EditorGUILayout.PropertyField(sceneList.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled"));
70 
71  GUI.enabled = false;
72 
73  string path = sceneList.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue;
74  var sceneAsset = AssetDatabase.LoadAssetAtPath(path, typeof(SceneAsset)) as SceneAsset;
75  EditorGUILayout.ObjectField(sceneAsset, typeof(SceneAsset), false);
76 
77  GUI.enabled = true;
78  EditorGUILayout.EndHorizontal();
79  }
80 
81  EditorGUI.indentLevel--;
82  }
83  }
84 
85  private void CheckBuildScenes(SerializedProperty list)
86  {
87  if (EditorBuildSettings.scenes.Length == 0)
88  {
89  return;
90  }
91 
92  bool reBuildList = list.arraySize != EditorBuildSettings.scenes.Length;
93 
94  if (!reBuildList)
95  {
96  // Check if the build settings list is the same as ours.
97  for (int i = 0; i < list.arraySize; i++)
98  {
99  if (list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue != EditorBuildSettings.scenes[i].path)
100  {
101  reBuildList = true;
102  }
103  }
104  }
105 
106  if (reBuildList)
107  {
108  // if it's not then we need to store a copy of our mappings.
109  var oldBuildSceneMapping = new EditorBuildSettingsScene[list.arraySize];
110  for (int i = 0; i < list.arraySize; i++)
111  {
112  oldBuildSceneMapping[i] = new EditorBuildSettingsScene
113  {
114  path = list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue,
115  enabled = list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue
116  };
117  }
118 
119  // Then re assign the mapping to the right scene in the build settings window.
120  list.ClearArray();
121 
122  for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
123  {
124  list.InsertArrayElementAtIndex(i);
125  list.GetArrayElementAtIndex(i).FindPropertyRelative("ScenePath").stringValue = EditorBuildSettings.scenes[i].path;
126  list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue = false;
127 
128  for (var j = 0; j < oldBuildSceneMapping.Length; j++)
129  {
130  if (oldBuildSceneMapping[j].path == EditorBuildSettings.scenes[i].path)
131  {
132  list.GetArrayElementAtIndex(i).FindPropertyRelative("IsButtonEnabled").boolValue = oldBuildSceneMapping[j].enabled;
133  }
134  }
135  }
136  }
137  }
138  }
139 }