Skip to content

Instantly share code, notes, and snippets.

@gfsl
Created August 3, 2013 06:19
Show Gist options
  • Save gfsl/6145427 to your computer and use it in GitHub Desktop.
Save gfsl/6145427 to your computer and use it in GitHub Desktop.
[Popup("String1", "String2", ...] Creates an Enum-style dropdown list of Strings. Attribute in \Attributes, Drawer in \Editor.
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class PopupAttribute : PropertyAttribute {
public List<string> optionsList;
public PopupAttribute(params string[] args) {
this.optionsList = new List<string>(args);
}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(PopupAttribute))]
public class PopupDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label) {
var attrib = attribute as PopupAttribute;
int index = attrib.optionsList.IndexOf(prop.stringValue);
if (index < 0) index = 0;
int newIndex = EditorGUI.Popup(position, label.text, index, attrib.optionsList.ToArray());
if (newIndex != index) prop.stringValue = attrib.optionsList[newIndex];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment