Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created April 25, 2024 19:07
Show Gist options
  • Save SiarheiPilat/4cfae74070d605d308db2cf4ffd2d5cb to your computer and use it in GitHub Desktop.
Save SiarheiPilat/4cfae74070d605d308db2cf4ffd2d5cb to your computer and use it in GitHub Desktop.
// Taken from: https://discussions.unity.com/t/inspector-field-for-scene-asset/40763
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[System.Serializable]
public class SceneField
{
[SerializeField]
private Object m_SceneAsset;
[SerializeField]
private string m_SceneName = "";
public string SceneName
{
get { return m_SceneName; }
}
// makes it work with the existing Unity methods (LoadLevel/LoadScene)
public static implicit operator string( SceneField sceneField )
{
return sceneField.SceneName;
}
}
#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);
SerializedProperty sceneAsset = _property.FindPropertyRelative("m_SceneAsset");
SerializedProperty sceneName = _property.FindPropertyRelative("m_SceneName");
_position = EditorGUI.PrefixLabel(_position, GUIUtility.GetControlID(FocusType.Passive), _label);
if (sceneAsset != null)
{
sceneAsset.objectReferenceValue = EditorGUI.ObjectField(_position, sceneAsset.objectReferenceValue, typeof(SceneAsset), false);
if( sceneAsset.objectReferenceValue != null )
{
sceneName.stringValue = (sceneAsset.objectReferenceValue as SceneAsset).name;
}
}
EditorGUI.EndProperty( );
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment