Show Certain Variables On Enum Selection

What I would like to do is to show certain variables on an enum selection. This works if Recipe.cs is attached to an object, but not if you make an array of those objects within RecipeManager.cs. Can this work at all? If so, how? If not, why? Thank you!

RecipeEditor:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Recipe))]
publicclassRecipeEditor:Editor{

publicSerializedProperty
recipeType_prop,
addWhipCream_prop,
addHotFudge_prop,
addCroutons_prop,
addBacon_prop,
addSeaSalt_prop,
servingDish_prop;

voidOnEnable(){
//Setupthe SerializedProperties
recipeType_prop=serializedObject.FindProperty("recipeType");
addWhipCream_prop=serializedObject.FindProperty("addWhipCream");
addHotFudge_prop=serializedObject.FindProperty("addHotFudge");
addCroutons_prop=serializedObject.FindProperty("addCroutons");
addBacon_prop=serializedObject.FindProperty("addBacon");
addSeaSalt_prop=serializedObject.FindProperty("addSeaSalt");
servingDish_prop=serializedObject.FindProperty("servingDish");
}

publicoverridevoidOnInspectorGUI(){
serializedObject.Update();

EditorGUILayout.PropertyField(recipeType_prop);

Recipe.RecipeTyperType=(Recipe.RecipeType)recipeType_prop.intValue;

switch(rType){
caseRecipe.RecipeType.Dessert:
EditorGUILayout.PropertyField(addWhipCream_prop);
EditorGUILayout.PropertyField(addHotFudge_prop);
EditorGUILayout.PropertyField(addBacon_prop);
EditorGUILayout.PropertyField(addSeaSalt_prop);
break;

caseRecipe.RecipeType.MainCourse:
EditorGUILayout.PropertyField(addBacon_prop);
EditorGUILayout.PropertyField(addSeaSalt_prop);
break;

caseRecipe.RecipeType.Salad:
EditorGUILayout.PropertyField(addCroutons_prop);
EditorGUILayout.PropertyField(addBacon_prop);
EditorGUILayout.PropertyField(addSeaSalt_prop);
break;

}

EditorGUILayout.PropertyField(servingDish_prop);

serializedObject.ApplyModifiedProperties();
}
}

Recipe:

using UnityEngine;
using System.Collections;

//[System.Serializable]
public class Recipe : MonoBehaviour
{
    public string name;

    [System.Serializable]
    public enum RecipeType { Dessert, Salad, MainCourse }
    public RecipeType recipeType;

    public bool addWhipCream;
    public bool addHotFudge;
    public bool addCroutons;
    public bool addBacon;
    public bool addSeaSalt;

    [System.Serializable]
    public enum ServingDish { Plate, SaladPlate, Bowl }
    public ServingDish servingDish;
}

RecipeManager:

using UnityEngine;
using System.Collections;

public class RecipeManager : MonoBehaviour {

    public string keyPrefix;

    public Recipe[] recipes;
}

Just write a custom editor where you convert the enum evalues you need to display to strings and put them into an array - then i OnInspectorGUI draw them by invoking UnityEditor.EditorGUILayout.Popup That will make it possible to show just the specific values that you want to do, when the selection index has changed then convert the stirng back to the enum agin…

Thanks for the reply crispybeans.
Below is what I ended up with after studying up on custom editors and property drawers. Not sure if it is optimal, but it works and quick to edit.

Editor

using UnityEditor;
using System.Collections.Generic;

[CustomEditor (typeof (myClassForEditor))]
public class myEditor : Editor {

    // manager properties
    public List<string> myVars = new List<string> ()
        {
            // Add variable names as strings to display here
        };
    public List<SerializedProperty> myProps = new List<SerializedProperty> ();

    // fold outs
    public Dictionary <string, bool> dArrayFoldOuts = new Dictionary<string, bool>()
    {
        // add arrays and lists variables to display here
        // string = variable name
        // bool : true = expanded by default, false = not expanded by default
    };


    void OnEnable () {

        foreach (string str in myVars.ToArray())
        {
            myProps.Add (serializedObject.FindProperty (str));
        }
    }

    public override void OnInspectorGUI() {

        serializedObject.Update ();

        foreach (SerializedProperty aProp in myProps)
        {
            EditorGUI.indentLevel = 0;
            if (dArrayFoldOuts.ContainsKey(aProp.name))
            {
                dArrayFoldOuts[aProp.name] = EditorGUILayout.Foldout(dArrayFoldOuts[aProp.name], aProp.name.ToUpper());

                if (dArrayFoldOuts[aProp.name])
                {
                    DrawArray (serializedObject, aProp);
                }
            }
            else
                EditorGUILayout.PropertyField( aProp, true );
        }

        serializedObject.ApplyModifiedProperties ();
    }

    void DrawArray(SerializedObject obj, SerializedProperty theArray)
    {

        // get the number of items of the array
        int size = theArray.arraySize;
        int newSize = EditorGUILayout.IntField("Quantity:", size);
        if (newSize != size)
        {
            theArray.arraySize = newSize;
        }

        // include all the properties for the current array item
        for (int i = 0; i < newSize; i++)
        {
            // set the indent
            EditorGUI.indentLevel = 2;
            EditorGUILayout.PropertyField (theArray.GetArrayElementAtIndex (i), true);
        }
    }
}

PropertyDrawer for Class Arrays

using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections.Generic;

[CustomPropertyDrawer (typeof(MyClassScriptName))]
public class MyClassNameDrawer : PropertyDrawer {

    // Inspector Variables
    public List<string> myClassVars = new List<string> ()
        {
            // Add variable names here
            "one",
            "two",
            "three",
            "four",
            "five",
            "six",
            "seven",
            "eight",
            "firstEnumVariableName",
            "secondEnumVariableName"
        };

    // First Enum
    public string myFirstEnum = "firstEnumVariableName";
    public List<string> lstFirstEnum = new List<string>();
    public Dictionary<string, List<string>> firstEnumOmits = new Dictionary<string, List<string>>()
    {
        // Add all your enum selections here
        // string = name of enum selection
        // List<string> = property fields to omit from the inspector based on the enum selection
        // Only Caveat is that ALL the enum selection names need to be unique
        { "EnumSel01" , new List<string> () { "three", "four" } },
        { "EnumSel02" , new List<string> () { "one", "two" } }
    }
    // Second Enum
    public string mySecondEnum = "secondEnumVariableName";
    public List<string> lstSecondEnum = new List<string>();
    public Dictionary<string, List<string>> secondEnumOmits = new Dictionary<string, List<string>>()
    {
        // Add all your enum selections here
        // string = name of enum selection
        // List<string> = property fields to omit from the inspector based on the enum selection
        // Only Caveat is that ALL the enum selection names need to be unique
        { "EnumSel01" , new List<string> () { "seven", "eight" } },
        { "EnumSel02" , new List<string> () { "five", "six" } }
    };


    public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label)
    {
        // Follow format and add additional enums here
        MyClass.MyFirstEnum firstSel = (MyClass.MyFirstEnum)prop.FindPropertyRelative(myFirstEnum).intValue;
        MyClass.MySecondEnum secondSel = (MyClass.MySecondEnum)prop.FindPropertyRelative(mySecondEnum).intValue;

        // remove variables by first enum selection
        lstFirstEnum = firstEnumOmits [firstSel.ToString ()];
        var result = myClassVars.Except (lstFirstEnum);

        // remove variables by second enum selection
        lstSecondEnum = secondEnumOmits [secondSel.ToString ()];
        result = result.Except (lstSecondEnum);

        EditorGUI.BeginProperty (pos, GUIContent.none, prop);

        prop.isExpanded = EditorGUI.Foldout(pos, prop.isExpanded, label);

        if (prop.isExpanded)
        {
            EditorGUI.indentLevel = 3;
            foreach (string str in result.ToArray())
            {
                EditorGUILayout.PropertyField (prop.FindPropertyRelative (str), true);
            }
        }

        EditorGUI.EndProperty ();
    }
}
1 Like