Last active
April 7, 2026 18:07
-
-
Save phosphoer/c9c8cd3e19576c001d589cdf514a2ef1 to your computer and use it in GitHub Desktop.
SceneField
This file contains hidden or 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
| // 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() { } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a cleaned up version with Odin that gets rid of the extra inspector fields.