Skip to content

Instantly share code, notes, and snippets.

@phosphoer
Last active April 7, 2026 18:07
Show Gist options
  • Select an option

  • Save phosphoer/c9c8cd3e19576c001d589cdf514a2ef1 to your computer and use it in GitHub Desktop.

Select an option

Save phosphoer/c9c8cd3e19576c001d589cdf514a2ef1 to your computer and use it in GitHub Desktop.
SceneField
// Taken from http://wiki.unity3d.com/index.php/SceneField
using UnityEngine;
[System.Serializable]
public class SceneField : ISerializationCallbackReceiver
{
public string SceneName => sceneName;
public string ScenePath => scenePath;
#if UNITY_EDITOR
public UnityEditor.SceneAsset sceneAsset;
#endif
#pragma warning disable 414
[SerializeField] private string sceneName = "";
[SerializeField] private string scenePath = "";
#pragma warning restore 414
// Makes it work with the existing Unity methods (LoadLevel/LoadScene)
public static implicit operator string(SceneField sceneField)
{
#if UNITY_EDITOR
return System.IO.Path.GetFileNameWithoutExtension(UnityEditor.AssetDatabase.GetAssetPath(sceneField.sceneAsset));
#else
return sceneField.sceneName;
#endif
}
#if UNITY_EDITOR
public void EditorSetScene(UnityEngine.SceneManagement.Scene scene)
{
sceneAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.SceneAsset>(scene.path);
sceneName = System.IO.Path.GetFileName(scene.path);
scenePath = scene.path;
}
#endif
public void OnBeforeSerialize()
{
#if UNITY_EDITOR
sceneName = this;
scenePath = UnityEditor.AssetDatabase.GetAssetPath(sceneAsset);
#endif
}
public void OnAfterDeserialize() { }
}
@ashblue
Copy link
Copy Markdown

ashblue commented Apr 7, 2026

Here is a cleaned up version with Odin that gets rid of the extra inspector fields.

using Sirenix.OdinInspector;
using UnityEngine;

// Taken from http://wiki.unity3d.com/index.php/SceneField
    [InlineProperty]
    [System.Serializable]
    public class SceneField : ISerializationCallbackReceiver {
        public string SceneName => _sceneName;
        public string ScenePath => _scenePath;

#if UNITY_EDITOR
        [HideLabel]
        public UnityEditor.SceneAsset _sceneAsset;
#endif

#pragma warning disable 414
        [HideInEditorMode]
        [ReadOnly]
        [SerializeField]
        private string _sceneName = "";

        [HideInEditorMode]
        [ReadOnly]
        [SerializeField]
        private string _scenePath = "";
#pragma warning restore 414


        // Makes it work with the existing Unity methods (LoadLevel/LoadScene)
        public static implicit operator string (SceneField sceneField) {
#if UNITY_EDITOR
            return System.IO.Path.GetFileNameWithoutExtension(
                UnityEditor.AssetDatabase.GetAssetPath(sceneField._sceneAsset)
            );
#else
    return sceneField.SceneName;
#endif
        }

#if UNITY_EDITOR
        public void EditorSetScene (UnityEngine.SceneManagement.Scene scene) {
            _sceneAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEditor.SceneAsset>(scene.path);
            _sceneName = System.IO.Path.GetFileName(scene.path);
            _scenePath = scene.path;
        }
#endif

        public void OnBeforeSerialize () {
#if UNITY_EDITOR
            _sceneName = this;
            _scenePath = UnityEditor.AssetDatabase.GetAssetPath(_sceneAsset);
#endif
        }

        public void OnAfterDeserialize () {
        }
    }

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