HKI Core
SceneDrawer.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 
4 namespace HKI.Core.Scenes.Editor
5 {
9  [CustomPropertyDrawer(typeof(SceneAttribute))]
10  public class SceneDrawer : PropertyDrawer
11  {
12  // OnGUI function
13  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
14  {
15 
16  if (property.propertyType == SerializedPropertyType.String)
17  {
18  var sceneObject = GetSceneObject(property.stringValue);
19  var scene = EditorGUI.ObjectField(position, label, sceneObject, typeof(SceneAsset), true);
20  if (scene == null)
21  {
22  property.stringValue = "";
23  }
24  else if (scene.name != property.stringValue)
25  {
26  var sceneObj = GetSceneObject(scene.name);
27  if (sceneObj == null)
28  {
29  Debug.LogWarning("The scene " + scene.name + " cannot be used. To use this scene add it to the build settings for the project");
30  }
31  else
32  {
33  property.stringValue = scene.name;
34  }
35  }
36  }
37  else
38  EditorGUI.LabelField(position, label.text, "Use [Scene] with strings.");
39  }
40 
41  // Protected function
42  protected SceneAsset GetSceneObject(string sceneObjectName)
43  {
44  if (string.IsNullOrEmpty(sceneObjectName))
45  {
46  return null;
47  }
48 
49  foreach (var editorScene in EditorBuildSettings.scenes)
50  {
51  if (editorScene.path.IndexOf(sceneObjectName) != -1)
52  {
53  return AssetDatabase.LoadAssetAtPath(editorScene.path, typeof(SceneAsset)) as SceneAsset;
54  }
55  }
56  Debug.LogWarning("Scene [" + sceneObjectName + "] cannot be used. Add this scene to the 'Scenes in the Build' in build settings.");
57  return null;
58  }
59  }
60 }
SceneAsset GetSceneObject(string sceneObjectName)
Definition: SceneDrawer.cs:42
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
Definition: SceneDrawer.cs:13
Custom property drawer for the SceneAttribute class.
Definition: SceneDrawer.cs:10
This PropertyAttribute is for using the scene object like other objects in Unity and draged to a Prop...