Skip to content

Instantly share code, notes, and snippets.

@Hertzole
Last active May 4, 2024 13:04
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save Hertzole/ac269f3148bc5192cc2eb6d472870d24 to your computer and use it in GitHub Desktop.
Save Hertzole/ac269f3148bc5192cc2eb6d472870d24 to your computer and use it in GitHub Desktop.
Unity scene object to easily assign scenes in the inspector.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[System.Serializable]
public class SceneObject
{
[SerializeField]
private string m_SceneName;
public static implicit operator string(SceneObject sceneObject)
{
return sceneObject.m_SceneName;
}
public static implicit operator SceneObject(string sceneName)
{
return new SceneObject() { m_SceneName = sceneName };
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneObject))]
public class SceneObjectEditor : PropertyDrawer
{
protected SceneAsset GetSceneObject(string sceneObjectName)
{
if (string.IsNullOrEmpty(sceneObjectName))
return null;
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
{
EditorBuildSettingsScene scene = EditorBuildSettings.scenes[i];
if (scene.path.IndexOf(sceneObjectName) != -1)
{
return AssetDatabase.LoadAssetAtPath(scene.path, typeof(SceneAsset)) as SceneAsset;
}
}
Debug.Log("Scene [" + sceneObjectName + "] cannot be used. Add this scene to the 'Scenes in the Build' in the build settings.");
return null;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var sceneObj = GetSceneObject(property.FindPropertyRelative("m_SceneName").stringValue);
var newScene = EditorGUI.ObjectField(position, label, sceneObj, typeof(SceneAsset), false);
if (newScene == null)
{
var prop = property.FindPropertyRelative("m_SceneName");
prop.stringValue = "";
}
else
{
if (newScene.name != property.FindPropertyRelative("m_SceneName").stringValue)
{
var scnObj = GetSceneObject(newScene.name);
if (scnObj == null)
{
Debug.LogWarning("The scene " + newScene.name + " cannot be used. To use this scene add it to the build settings for the project.");
}
else
{
var prop = property.FindPropertyRelative("m_SceneName");
prop.stringValue = newScene.name;
}
}
}
}
}
#endif
@AbhimanyuAryan
Copy link

Nice

@SatoMasato29
Copy link

thankyou! i wanted this

@Kuritsu243
Copy link

needed this! thank you!!

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