Skip to content

Instantly share code, notes, and snippets.

@KarlRamstedt
Created January 4, 2020 16:34
Show Gist options
  • Save KarlRamstedt/d325fe6e4a2221d58775d65a2f8c94f0 to your computer and use it in GitHub Desktop.
Save KarlRamstedt/d325fe6e4a2221d58775d65a2f8c94f0 to your computer and use it in GitHub Desktop.
Unity Scene Reference Attribute
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 {}
}
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();
}
}
}
@KarlRamstedt
Copy link
Author

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 your Assets folder, SceneRefDrawer.cs needs to be placed in a folder named Editor (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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment