SceneField.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. [System.Serializable]
  6. public class SceneField
  7. {
  8. public Object SceneAsset { get { return m_SceneAsset; } }
  9. [SerializeField]
  10. private Object m_SceneAsset = null;
  11. [SerializeField]
  12. private string m_SceneName = "";
  13. public string SceneName
  14. {
  15. get { return m_SceneName; }
  16. }
  17. // makes it work with the existing Unity methods (LoadLevel/LoadScene)
  18. public static implicit operator string( SceneField sceneField )
  19. {
  20. return sceneField.SceneName;
  21. }
  22. }
  23. #if UNITY_EDITOR
  24. [CustomPropertyDrawer(typeof(SceneField))]
  25. public class SceneFieldPropertyDrawer : PropertyDrawer
  26. {
  27. public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
  28. {
  29. EditorGUI.BeginProperty(_position, GUIContent.none, _property);
  30. SerializedProperty sceneAsset = _property.FindPropertyRelative("m_SceneAsset");
  31. SerializedProperty sceneName = _property.FindPropertyRelative("m_SceneName");
  32. _position = EditorGUI.PrefixLabel(_position, GUIUtility.GetControlID(FocusType.Passive), _label);
  33. if (sceneAsset != null)
  34. {
  35. sceneAsset.objectReferenceValue = EditorGUI.ObjectField(_position, sceneAsset.objectReferenceValue, typeof(SceneAsset), false);
  36. if( sceneAsset.objectReferenceValue != null )
  37. {
  38. sceneName.stringValue = (sceneAsset.objectReferenceValue as SceneAsset).name;
  39. }
  40. }
  41. EditorGUI.EndProperty( );
  42. }
  43. }
  44. #endif