Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomsseisums/794b113b1d9fc2df54e890f123f63312 to your computer and use it in GitHub Desktop.
Save tomsseisums/794b113b1d9fc2df54e890f123f63312 to your computer and use it in GitHub Desktop.
Scene picker for Unity3D
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(SceneAssetPathField))]
public class SceneAssetPathFieldPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var oldPath = AssetDatabase.LoadAssetAtPath<SceneAsset>(property.stringValue);
position = EditorGUI.PrefixLabel(position, new GUIContent(property.displayName));
EditorGUI.BeginChangeCheck();
var newScene = EditorGUI.ObjectField(position, oldPath, typeof(SceneAsset), false) as SceneAsset;
if (EditorGUI.EndChangeCheck())
{
var newPath = AssetDatabase.GetAssetPath(newScene);
property.stringValue = newPath;
}
}
}
using UnityEngine;
public class SceneAssetPathField : PropertyAttribute
{
// While there are various options to tweak the code above
// to suit other needs (store only name, loadable path, maybe even index - requires editor script rewrite, though)
// this helper comes in handy for the version found here.
// Useful because in editor, the scene is saved with Assets/ prefix an *.unity suffix, but
// in build the prefix is removed and SceneManager works without extension.
// Not a fully optimized one, but hey - this is just a tip! ;)
public static string LoadableName(string path)
{
string start = "Assets/";
string end = ".unity";
if (path.EndsWith(end))
{
path = path.Substring(0, path.LastIndexOf(end));
}
if (start.StartsWith(start))
{
path = path.Substring(start.Length);
}
return path;
}
}
using UnityEngine;
public class SceneReferencingObject : MonoBehaviour
{
[SerializeField, SceneAssetPathField]
private string referencedScenePath;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment