Last active
May 24, 2018 02:42
-
-
Save tomsseisums/794b113b1d9fc2df54e890f123f63312 to your computer and use it in GitHub Desktop.
Scene picker for Unity3D
This file contains 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
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; | |
} | |
} | |
} |
This file contains 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
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; | |
} | |
} |
This file contains 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
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