Created
January 4, 2020 16:34
-
-
Save KarlRamstedt/d325fe6e4a2221d58775d65a2f8c94f0 to your computer and use it in GitHub Desktop.
Unity Scene Reference Attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace ModularOptions { | |
/// <summary> | |
/// Uses a CustomPropertyDrawer to draw a string field as a SceneAsset object field. | |
/// </summary> | |
/// <example><code> | |
/// [SceneRef] string mainMenu; | |
/// </code></example> | |
[System.AttributeUsage(System.AttributeTargets.Field)] | |
public sealed class SceneRefAttribute : PropertyAttribute {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
namespace ModularOptions { | |
/// <summary> | |
/// Shows a string variable field as a SceneAsset object reference field. | |
/// Fully supports standard built-in Editor features such as Undo, through the use of SerializedProperties. | |
/// </summary> | |
[CustomPropertyDrawer(typeof(SceneRefAttribute))] | |
public class SceneRefDrawer : PropertyDrawer { | |
public override void OnGUI(Rect _rect, SerializedProperty _prop, GUIContent _label){ | |
if (_prop.propertyType != SerializedPropertyType.String){ | |
EditorGUI.HelpBox(_rect, "Error: SceneRefAttribute only works on strings", MessageType.Error); | |
return; | |
} | |
var selectedScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(_prop.stringValue); | |
_label = EditorGUI.BeginProperty(_rect, _label, _prop); | |
EditorGUI.BeginChangeCheck(); | |
var newScene = EditorGUI.ObjectField(_rect, _label, selectedScene, typeof(SceneAsset), false); | |
if (EditorGUI.EndChangeCheck()){ | |
_prop.stringValue = AssetDatabase.GetAssetPath(newScene); | |
} | |
EditorGUI.EndProperty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a way to avoid the pain of having to select scenes by numbers or strings. Just drag the scene you wanna use to the object reference field, or, of course, use the object picker (circled dot to the right of object fields).
Installation:
SceneRefAttribute.cs
can be put anywhere in yourAssets
folder,SceneRefDrawer.cs
needs to be placed in a folder namedEditor
(anywhere in your Assets). Then just use[SceneRef]
before any string you use want to use as a scene reference.Note: Renaming or moving scenes will break references as the SceneAsset is being tracked using its path.