-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneField.cs
77 lines (62 loc) · 2.77 KB
/
SceneField.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
using System;
using System.IO;
using UnityEngine;
using Object = UnityEngine.Object;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace TinyMatter.Core {
//source https://answers.unity.com/questions/242794/inspector-field-for-scene-asset.html
[System.Serializable]
public class SceneField {
[SerializeField] private Object sceneAsset;
[SerializeField] private string scenePath;
private SceneField(string s) {
scenePath = s;
}
public string ScenePath => scenePath;
public string FullScenePath => $"Assets/{ScenePath}.unity";
public string SceneName => Path.GetFileName(scenePath);
#if UNITY_EDITOR
public void SetPath(string path) {
var fullPath = $"Assets/{path}.unity";
var asset = AssetDatabase.LoadAssetAtPath(fullPath, typeof(SceneAsset));
sceneAsset = asset;
scenePath = path;
}
#endif
// makes it work with the existing Unity methods (LoadLevel/LoadScene)
public static implicit operator string(SceneField sceneField) {
return sceneField.ScenePath;
}
public static implicit operator SceneField(string scenePath) {
return new SceneField(scenePath);
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneField))]
public class SceneFieldPropertyDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty(position, GUIContent.none, property);
var sceneAsset = property.FindPropertyRelative("sceneAsset");
var sceneName = property.FindPropertyRelative("scenePath");
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
if (sceneAsset != null) {
EditorGUI.BeginChangeCheck();
var value = EditorGUI.ObjectField(position, sceneAsset.objectReferenceValue, typeof(SceneAsset), false);
if (EditorGUI.EndChangeCheck()) {
sceneAsset.objectReferenceValue = value;
if (sceneAsset.objectReferenceValue != null) {
var scenePath = AssetDatabase.GetAssetPath(sceneAsset.objectReferenceValue);
var assetsIndex = scenePath.IndexOf("Assets", StringComparison.Ordinal) + 7;
var extensionIndex = scenePath.LastIndexOf(".unity", StringComparison.Ordinal);
scenePath = scenePath.Substring(assetsIndex, extensionIndex - assetsIndex);
sceneName.stringValue = scenePath;
}
}
}
EditorGUI.EndProperty();
}
}
#endif
}