Created
August 3, 2013 06:19
-
-
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.
This file contains hidden or 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; | |
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); | |
} | |
} |
This file contains hidden or 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; | |
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