-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSceneReference.cs
More file actions
115 lines (96 loc) · 3.24 KB
/
SceneReference.cs
File metadata and controls
115 lines (96 loc) · 3.24 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
using System;
using System.Linq;
using Sirenix.OdinInspector;
using Sirenix.Utilities;
using UnityEngine;
namespace Screenplay
{
[Serializable, InlineProperty]
public struct SceneReference : ISerializationCallbackReceiver, IEquatable<SceneReference>
{
public string Path => _path ?? "";
[SerializeField, HideInInspector] private string? _path;
#if UNITY_EDITOR
[SerializeField, HideLabel]
private UnityEditor.SceneAsset _sceneAsset;
public SceneReference(UnityEditor.SceneAsset asset)
{
_sceneAsset = asset;
_path = UnityEditor.AssetDatabase.GetAssetPath(asset);
}
private bool SceneNotNull(UnityEditor.SceneAsset asset, ref string message)
{
if (asset == null)
{
_path = null;
message = "Scene must not be null";
return false;
}
_path = UnityEditor.AssetDatabase.GetAssetPath(asset);
bool found = false;
foreach (var scene in UnityEditor.EditorBuildSettings.scenes)
if (_path == scene.path)
found = true;
if (found == false)
{
message = $"Scene {asset.name} is not part of the build";
return false;
}
return true;
}
private bool SceneNotPartOfBuild()
{
if (_sceneAsset == null)
return false;
_path = UnityEditor.AssetDatabase.GetAssetPath(_sceneAsset);
foreach (var scene in UnityEditor.EditorBuildSettings.scenes)
if (_path == scene.path)
return false;
return true;
}
[ShowIf(nameof(SceneNotPartOfBuild))]
[Button]
private void AddSceneToBuild()
{
UnityEditor.EditorBuildSettings.scenes = UnityEditor.EditorBuildSettings.scenes
.Append(new UnityEditor.EditorBuildSettingsScene(_path, true))
.ToArray();
}
#endif
public void OnBeforeSerialize()
{
#if UNITY_EDITOR
// Make sure the path is always updated
if (_sceneAsset != null)
_path = UnityEditor.AssetDatabase.GetAssetPath(_sceneAsset);
#endif
}
public void OnAfterDeserialize(){ }
public bool IsValid()
{
return _path != null;
}
public override bool Equals(object? obj)
{
if (obj is SceneReference other)
return Equals(other);
return base.Equals(obj);
}
public bool Equals(SceneReference other)
{
#if UNITY_EDITOR
if (other._sceneAsset == this._sceneAsset)
return true;
#endif
if (other._path == _path)
return true;
if (other._path.IsNullOrWhitespace() && _path.IsNullOrWhitespace())
return true; // Edge case, unity serializes null string as empty string, we still want those two to match between themselves
return false;
}
public override int GetHashCode()
{
return _path.IsNullOrWhitespace() ? "".GetHashCode() : _path!.GetHashCode();
}
}
}