Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created May 19, 2015 02:10
Show Gist options
  • Save rutcreate/b02738c5d81dc9811e79 to your computer and use it in GitHub Desktop.
Save rutcreate/b02738c5d81dc9811e79 to your computer and use it in GitHub Desktop.
Unity3D Editor: Scene popup
using UnityEngine;
using System.Collections;
public class ScenePopup : PropertyAttribute {
public bool required = false;
public ScenePopup() {}
public ScenePopup(bool required) {
this.required = required;
}
}
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
[CustomPropertyDrawer(typeof(ScenePopup))]
public class ScenePopupDrawer : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
if (property.propertyType != SerializedPropertyType.Integer) {
EditorGUI.PropertyField(position, property);
return;
}
List<GUIContent> sceneNames = new List<GUIContent>();
List<int> sceneValues = new List<int>();
ScenePopup scenePopup = attribute as ScenePopup;
if (!scenePopup.required) {
sceneNames.Add(new GUIContent("- None -"));
sceneValues.Add(-1);
}
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) {
EditorBuildSettingsScene scene = EditorBuildSettings.scenes[i];
if (scene.enabled) {
string name = Path.GetFileName(scene.path);
sceneNames.Add(new GUIContent(name));
sceneValues.Add(i);
}
}
EditorGUI.IntPopup(position, property, sceneNames.ToArray(), sceneValues.ToArray());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment