This repository was archived by the owner on May 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathSceneVariable.cs
More file actions
138 lines (119 loc) · 4.19 KB
/
SceneVariable.cs
File metadata and controls
138 lines (119 loc) · 4.19 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
using System;
using UnityEngine;
using UnityEngine.Events;
namespace ScriptableObjectArchitecture
{
[System.Serializable]
public class SceneInfoEvent : UnityEvent<SceneInfo> { }
/// <summary>
/// <see cref="SceneVariable"/> is a scriptable constant variable whose scene values are assigned at
/// edit-time by assigning a <see cref="UnityEditor.SceneAsset"/> instance to it.
/// </summary>
[CreateAssetMenu(
fileName = "SceneVariable.asset",
menuName = SOArchitecture_Utility.ADVANCED_VARIABLE_SUBMENU + "Scene",
order = 120)]
public sealed class SceneVariable : BaseVariable<SceneInfo, SceneInfoEvent>
{
/// <summary>
/// Returns the <see cref="SceneInfo"/> of this instance.
/// </summary>
public override SceneInfo Value
{
get { return _value; }
}
public override bool ReadOnly
{
get
{
// A scene variable is essentially a constant for edit-time modification only; there is not
// any kind of expectation for a user to be able to set this at runtime.
return true;
}
}
}
[Serializable]
[MultiLine]
public sealed class SceneInfo : ISerializationCallbackReceiver
{
/// <summary>
/// Returns the fully-qualified name of the scene.
/// </summary>
public string SceneName
{
get { return _sceneName; }
}
/// <summary>
/// Returns the index of the scene in the build settings; if not present, -1 will be returned instead.
/// </summary>
public int SceneIndex
{
get { return _sceneIndex; }
internal set { _sceneIndex = value; }
}
/// <summary>
/// Returns true if the scene is present in the build settings, otherwise false.
/// </summary>
public bool IsSceneInBuildSettings
{
get { return _sceneIndex != -1; }
}
/// <summary>
/// Returns true if the scene is enabled in the build settings, otherwise false.
/// </summary>
public bool IsSceneEnabled
{
get { return _isSceneEnabled; }
internal set { _isSceneEnabled = value; }
}
#if UNITY_EDITOR
internal UnityEditor.SceneAsset Scene
{
get { return UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.SceneAsset>(_sceneName); }
}
#endif
#pragma warning disable 0649
[SerializeField]
private string _sceneName;
[SerializeField]
private int _sceneIndex;
[SerializeField]
private bool _isSceneEnabled;
#pragma warning restore 0649
public SceneInfo()
{
_sceneIndex = -1;
}
#region ISerializationCallbackReceiver
public void OnBeforeSerialize()
{
#if UNITY_EDITOR
if (Scene != null)
{
var sceneAssetPath = UnityEditor.AssetDatabase.GetAssetPath(Scene);
var sceneAssetGUID = UnityEditor.AssetDatabase.AssetPathToGUID(sceneAssetPath);
var scenes = UnityEditor.EditorBuildSettings.scenes;
SceneIndex = -1;
int enabledSceneIndex = 0;//scenes are only given a build index if enabled.
for (var i = 0; i < scenes.Length; i++)
{
bool sceneIsEnabled = scenes[i].enabled;
if (scenes[i].guid.ToString() == sceneAssetGUID)
{
if(sceneIsEnabled)
SceneIndex = enabledSceneIndex++;
IsSceneEnabled = sceneIsEnabled;
break;
}
else if (sceneIsEnabled)
{
++enabledSceneIndex;
}
}
}
#endif
}
public void OnAfterDeserialize(){}
#endregion
}
}