[Solved]Using Popup Property Drawer for Enumerations with special symbols

I’m looking for a way to create enumerations with special symbols. And the easiest way I’ve found to create a list or Popup from Inspector has been: Property Drawers in Unity 4.

But, this code:

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour {

    [Popup ("Warrior", "Mage", "Archer", "Ninja")]
    public string @class = "Warrior";

}

It throws the following errors:

I have installed 4.5.1f3 Unity version. Any solution?



[SOLVED]

Enumeration class, type to define enumerations from items field:

using UnityEngine;

public class Enumeration{

    public static readonly string[] items;

}

//Example 1
public class CameraResolutions : Enumeration{

    public static readonly new string[] items = new string[]{ "Free Aspect","5:4","4:3","3:2","16:10","16:9" };

}

//Example 2
public class Fraction4 : Enumeration{

    public static readonly new string[] items = new string[]{ "1\\4","2\\4","3\\4","4\\4" };

}

PropertyAttribute class, with class type and string params constructor:

using UnityEngine;

public class Enum : PropertyAttribute{

    public readonly string[] items;
    public int selected = 0;

    public Enum(System.Type type){

        if (type.IsSubclassOf(typeof(Enumeration)))
        {
            System.Reflection.FieldInfo fieldInfo = type.GetField("items");
            this.items = (string[])fieldInfo.GetValue (null);

        } else {

            this.items = new string[]{"Assign Enumeration Type"};

        }  
  
    }

    public Enum(params string[] enumerations){ this.items = enumerations; }

}

PropertyDrawer class, in Editor folder, to create popup list:

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer (typeof (Enum))]
public class EnumDrawer : PropertyDrawer {
  
    Enum enumeration{ get{ return (Enum)attribute; } }
  
    bool Start = true;
  
    public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label) {
      
        if(Start){
          
            Start = false;
            for(int i=0;i<enumeration.items.Length;i++){
              
                if(enumeration.items[i].Equals(prop.stringValue)){
                  
                    enumeration.selected = i;
                    break;
                  
                }
              
            }
          
        }
      
        enumeration.selected = EditorGUI.Popup(EditorGUI.PrefixLabel(position, label),enumeration.selected,enumeration.items);
        prop.stringValue = enumeration.items[enumeration.selected];
      
    }
      
}

Script Test:

using UnityEngine;

public class EnumerationTest : MonoBehaviour {

    [Enum("Free Aspect","5:4","4:3","3:2","16:10","16:9")]
    public string cameraResolution = "3:2";

    [Enum("1\\4","2\\4","3\\4","4\\4")]
    public string fraction;

    [Enum(typeof(CameraResolutions))]
    public string camResolution;

    [Enum(typeof(Fraction4))]
    public string frac="4\\4";


    void Start(){

        Debug.Log(cameraResolution);
        Debug.Log(fraction);
        Debug.Log(camResolution);
        Debug.Log(frac);

    }

}

Popup is not a built-in attribute
you have to script it yourself
That page tells you how to create a regexAttribute, now go make a popupAttribute

Ok. I tested this code.

This is Enumeration class, PropertyAttribute derived:

using UnityEngine;

public class Enumeration : PropertyAttribute {
   
    public readonly string[] items;
    public int selected = 0;
   
    public Enumeration(string[] enumerations){ this.items = enumerations; }

}

This is EnumerationDrawer class, PropertyDrawer derived:

using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer (typeof (Enumeration))]
public class EnumerationDrawer : PropertyDrawer {

    Enumeration enumeration{ get{ return (Enumeration)attribute; } }

    public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label) {

        enumeration.selected = EditorGUI.Popup(EditorGUI.PrefixLabel(position, label),enumeration.selected,enumeration.items);

    }

}

And this is EnumerationTest:

using UnityEngine;

public class EnumerationTest : MonoBehaviour {

    [Enumeration(new string[]{ "Free Aspect","5:4","4:3","3:2","16:10","16:9" })]
    public string cameraResolution;

    [Enumeration(new string[]{ "1/4","2/4","3/4","4/4" })]
    public string fraction;

    void Start(){

        Debug.Log(cameraResolution);
        Debug.Log(fraction);

    }

}

Result this:

Questions:

  1. How to write symbol /. The image show two panels or menus.
  2. Debug.log considers the string variables as objects.
  3. In example: Property Drawers in Unity 4, Popup attribute use indefined number of arguments, using comma-separated string.

1 Maybe use \ for a slash, dont think there is an escape for a forward slash

2 Everything from Debug.log is an object. Your variables are strings

3 Change line 8 your constructor to use params like (can’t test atm):

    public Enumeration(params string[] enumerations){ this.items = enumerations; }

Thanks for advice.

I have changed / symbol by \, and added ‘params’ to constructor.

To change string value, I returned to study the example, and I have seen the need to access the prop (SerializedProperty value) in PropertyDrawer OnGUI function.