-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSceneReference.cs
More file actions
292 lines (244 loc) · 8.22 KB
/
SceneReference.cs
File metadata and controls
292 lines (244 loc) · 8.22 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.SceneManagement;
#endif
namespace Jackey.Utilities.Unity {
/// <summary>
/// Save a reference to a scene asset
/// </summary>
[Serializable]
public struct SceneReference : ISerializationCallbackReceiver, IEquatable<SceneReference> {
#if UNITY_EDITOR
[SerializeField] private string m_sceneGuid;
#endif
[SerializeField] private int m_buildIndex;
/// <summary>
/// The build index of the referenced scene
/// </summary>
public int BuildIndex {
get {
#if UNITY_EDITOR
return GetBuildIndex();
#else
return m_buildIndex;
#endif
}
}
/// <summary>
/// Is the reference valid?
/// </summary>
public bool IsValid {
get {
#if UNITY_EDITOR
return GetBuildIndex() != -1;
#else
return m_buildIndex != -1;
#endif
}
}
public SceneReference(int buildIndex) : this() {
m_buildIndex = buildIndex;
}
public SceneReference(Scene scene) : this() {
m_buildIndex = scene.buildIndex;
}
#region Serialization
public void OnBeforeSerialize() {
#if UNITY_EDITOR
m_buildIndex = GetBuildIndex();
#endif
}
public void OnAfterDeserialize() { }
#endregion
/// <summary>
/// Load the referenced scene
/// </summary>
public void Load() {
if (!IsValid) {
Debug.LogWarning("Unable to load scene. Reference is invalid");
return;
}
#if UNITY_EDITOR
SceneManager.LoadScene(GetBuildIndex());
#else
SceneManager.LoadScene(m_buildIndex);
#endif
}
/// <summary>
/// Load the referenced scene using the specified load mode
/// </summary>
public void Load(LoadSceneMode loadMode) {
if (!IsValid) {
Debug.LogWarning("Unable to load scene. Reference is invalid");
return;
}
#if UNITY_EDITOR
SceneManager.LoadScene(GetBuildIndex(), loadMode);
#else
SceneManager.LoadScene(m_buildIndex, loadMode);
#endif
}
/// <summary>
/// Begin asynchronous loading of the referenced scene
/// </summary>
/// <returns>Returns the scene's load operation</returns>
public AsyncOperation LoadAsync() {
if (!IsValid) {
Debug.LogWarning("Unable to load scene. Reference is invalid");
return null;
}
#if UNITY_EDITOR
return SceneManager.LoadSceneAsync(GetBuildIndex());
#else
return SceneManager.LoadSceneAsync(m_buildIndex);
#endif
}
/// <summary>
/// Begin asynchronous loading of the referenced scene using the specified load mode
/// </summary>
/// <returns>Returns the scene's load operation</returns>
public AsyncOperation LoadAsync(LoadSceneMode loadMode) {
if (!IsValid) {
Debug.LogWarning("Unable to load scene. Reference is invalid");
return null;
}
#if UNITY_EDITOR
return SceneManager.LoadSceneAsync(GetBuildIndex(), loadMode);
#else
return SceneManager.LoadSceneAsync(m_buildIndex, loadMode);
#endif
}
#if UNITY_EDITOR
private int GetBuildIndex() {
if (string.IsNullOrEmpty(m_sceneGuid))
return -1;
string sceneAssetPath = AssetDatabase.GUIDToAssetPath(m_sceneGuid);
return SceneUtility.GetBuildIndexByScenePath(sceneAssetPath);
}
#endif
public override string ToString() {
#if UNITY_EDITOR
return GetBuildIndex().ToString();
#else
return m_buildIndex.ToString();
#endif
}
public static implicit operator int(SceneReference reference) {
#if UNITY_EDITOR
return reference.GetBuildIndex();
#else
return reference.m_buildIndex;
#endif
}
public static bool operator ==(SceneReference lhs, SceneReference rhs) => lhs.Equals(rhs);
public static bool operator !=(SceneReference lhs, SceneReference rhs) => !lhs.Equals(rhs);
public override bool Equals(object obj) => obj is SceneReference other && Equals(other);
public bool Equals(SceneReference other) {
#if UNITY_EDITOR
return m_sceneGuid == other.m_sceneGuid;
#else
return m_buildIndex == other.m_buildIndex;
#endif
}
public override int GetHashCode() {
#if UNITY_EDITOR
return m_sceneGuid.GetHashCode();
#else
return m_buildIndex;
#endif
}
}
namespace PropertyDrawers {
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneReference))]
public class SceneReferencePropertyDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
Color defaultGUIBackgroundColor = GUI.backgroundColor;
label = EditorGUI.BeginProperty(position, label, property);
SerializedProperty guidProperty = property.FindPropertyRelative("m_sceneGuid");
string sceneGuid = guidProperty.stringValue;
string scenePath = null;
SceneAsset sceneAsset = null;
EditorBuildSettingsScene buildScene = null;
if (!string.IsNullOrEmpty(sceneGuid)) {
scenePath = AssetDatabase.GUIDToAssetPath(sceneGuid);
if (!string.IsNullOrEmpty(scenePath)) {
sceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scenePath);
buildScene = EditorBuildSettings.scenes.FirstOrDefault(x =>
!string.IsNullOrEmpty(x.path) && x.guid.ToString() == sceneGuid
);
if (buildScene == null) {
GUI.backgroundColor = Color.red;
label.tooltip = "Scene is not present in build settings";
}
else if (!buildScene.enabled) {
GUI.backgroundColor = Color.yellow;
label.tooltip = "Scene is disabled in build settings";
}
}
}
position = EditorGUI.PrefixLabel(position, label);
if (sceneAsset != null && CheckContextMenu(position)) {
CreateContextMenu(scenePath, sceneAsset, buildScene);
Event.current.Use();
}
EditorGUI.BeginChangeCheck();
Object fieldValue = EditorGUI.ObjectField(position, GUIContent.none, sceneAsset, typeof(SceneAsset), false);
if (EditorGUI.EndChangeCheck()) {
string assetPath = AssetDatabase.GetAssetPath(fieldValue);
guidProperty.stringValue = AssetDatabase.GUIDFromAssetPath(assetPath).ToString();
}
EditorGUI.EndProperty();
GUI.backgroundColor = defaultGUIBackgroundColor;
}
private bool CheckContextMenu(Rect position) {
Event evt = Event.current;
return evt.type == EventType.MouseDown && evt.button == 1 && position.Contains(evt.mousePosition);
}
private void CreateContextMenu(string scenePath, SceneAsset sceneAsset, EditorBuildSettingsScene buildScene) {
GenericMenu menu = new GenericMenu();
if (!EditorApplication.isPlayingOrWillChangePlaymode) {
menu.AddItem(new GUIContent("Open Scene"), false, () => {
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorSceneManager.OpenScene(scenePath);
});
menu.AddItem(new GUIContent("Add Scene"), false, () => EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive));
menu.AddSeparator("");
}
if (buildScene != null) {
menu.AddItem(new GUIContent("Remove from Build Settings"), false, () => RemoveFromBuildSettings(buildScene));
menu.AddItem(new GUIContent("Enabled in Build Settings"), buildScene.enabled, () => ToggleBuildEnabled(buildScene));
}
else {
menu.AddItem(new GUIContent("Add to Build Settings"), false, () => AddToBuildSettings(scenePath));
}
menu.AddSeparator("");
menu.AddItem(new GUIContent("Properties..."), false, () => EditorUtility.OpenPropertyEditor(sceneAsset));
menu.ShowAsContext();
}
private static void AddToBuildSettings(string scenePath) {
EditorBuildSettingsScene buildScene = new EditorBuildSettingsScene(scenePath, true);
EditorBuildSettings.scenes = EditorBuildSettings.scenes.Append(buildScene).ToArray();
}
private static void RemoveFromBuildSettings(EditorBuildSettingsScene buildScene) {
List<EditorBuildSettingsScene> buildScenes = EditorBuildSettings.scenes.ToList();
int buildSceneIndex = buildScenes.FindIndex(x => x.guid == buildScene.guid);
buildScenes.RemoveAt(buildSceneIndex);
EditorBuildSettings.scenes = buildScenes.ToArray();
}
private static void ToggleBuildEnabled(EditorBuildSettingsScene buildScene) {
EditorBuildSettingsScene[] editorBuildScenes = EditorBuildSettings.scenes;
EditorBuildSettingsScene editorBuildScene = Array.Find(editorBuildScenes, x => x.guid == buildScene.guid);
editorBuildScene.enabled = !editorBuildScene.enabled;
EditorBuildSettings.scenes = editorBuildScenes;
}
}
#endif
}
}