Search Unity

Show Certain Variables On Enum Selection

Discussion in 'Immediate Mode GUI (IMGUI)' started by kdoug, Apr 29, 2016.

  1. kdoug

    kdoug

    Joined:
    Jan 15, 2013
    Posts:
    7
    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:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(Recipe))]
    6. publicclassRecipeEditor:Editor{
    7.  
    8. publicSerializedProperty
    9. recipeType_prop,
    10. addWhipCream_prop,
    11. addHotFudge_prop,
    12. addCroutons_prop,
    13. addBacon_prop,
    14. addSeaSalt_prop,
    15. servingDish_prop;
    16.  
    17. voidOnEnable(){
    18. //Setupthe SerializedProperties
    19. recipeType_prop=serializedObject.FindProperty("recipeType");
    20. addWhipCream_prop=serializedObject.FindProperty("addWhipCream");
    21. addHotFudge_prop=serializedObject.FindProperty("addHotFudge");
    22. addCroutons_prop=serializedObject.FindProperty("addCroutons");
    23. addBacon_prop=serializedObject.FindProperty("addBacon");
    24. addSeaSalt_prop=serializedObject.FindProperty("addSeaSalt");
    25. servingDish_prop=serializedObject.FindProperty("servingDish");
    26. }
    27.  
    28. publicoverridevoidOnInspectorGUI(){
    29. serializedObject.Update();
    30.  
    31. EditorGUILayout.PropertyField(recipeType_prop);
    32.  
    33. Recipe.RecipeTyperType=(Recipe.RecipeType)recipeType_prop.intValue;
    34.  
    35. switch(rType){
    36. caseRecipe.RecipeType.Dessert:
    37. EditorGUILayout.PropertyField(addWhipCream_prop);
    38. EditorGUILayout.PropertyField(addHotFudge_prop);
    39. EditorGUILayout.PropertyField(addBacon_prop);
    40. EditorGUILayout.PropertyField(addSeaSalt_prop);
    41. break;
    42.  
    43. caseRecipe.RecipeType.MainCourse:
    44. EditorGUILayout.PropertyField(addBacon_prop);
    45. EditorGUILayout.PropertyField(addSeaSalt_prop);
    46. break;
    47.  
    48. caseRecipe.RecipeType.Salad:
    49. EditorGUILayout.PropertyField(addCroutons_prop);
    50. EditorGUILayout.PropertyField(addBacon_prop);
    51. EditorGUILayout.PropertyField(addSeaSalt_prop);
    52. break;
    53.  
    54. }
    55.  
    56. EditorGUILayout.PropertyField(servingDish_prop);
    57.  
    58. serializedObject.ApplyModifiedProperties();
    59. }
    60. }
    61.  
    Recipe:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //[System.Serializable]
    5. public class Recipe : MonoBehaviour
    6. {
    7.     public string name;
    8.  
    9.     [System.Serializable]
    10.     public enum RecipeType { Dessert, Salad, MainCourse }
    11.     public RecipeType recipeType;
    12.  
    13.     public bool addWhipCream;
    14.     public bool addHotFudge;
    15.     public bool addCroutons;
    16.     public bool addBacon;
    17.     public bool addSeaSalt;
    18.  
    19.     [System.Serializable]
    20.     public enum ServingDish { Plate, SaladPlate, Bowl }
    21.     public ServingDish servingDish;
    22. }
    23.  
    RecipeManager:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RecipeManager : MonoBehaviour {
    5.  
    6.     public string keyPrefix;
    7.  
    8.     public Recipe[] recipes;
    9. }
    10.  
     
    Last edited: Apr 29, 2016
  2. crispybeans

    crispybeans

    Joined:
    Apr 13, 2015
    Posts:
    210
    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....
     
  3. kdoug

    kdoug

    Joined:
    Jan 15, 2013
    Posts:
    7
    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
    Code (CSharp):
    1. using UnityEditor;
    2. using System.Collections.Generic;
    3.  
    4. [CustomEditor (typeof (myClassForEditor))]
    5. public class myEditor : Editor {
    6.  
    7.     // manager properties
    8.     public List<string> myVars = new List<string> ()
    9.         {
    10.             // Add variable names as strings to display here
    11.         };
    12.     public List<SerializedProperty> myProps = new List<SerializedProperty> ();
    13.  
    14.     // fold outs
    15.     public Dictionary <string, bool> dArrayFoldOuts = new Dictionary<string, bool>()
    16.     {
    17.         // add arrays and lists variables to display here
    18.         // string = variable name
    19.         // bool : true = expanded by default, false = not expanded by default
    20.     };
    21.  
    22.  
    23.     void OnEnable () {
    24.  
    25.         foreach (string str in myVars.ToArray())
    26.         {
    27.             myProps.Add (serializedObject.FindProperty (str));
    28.         }
    29.     }
    30.  
    31.     public override void OnInspectorGUI() {
    32.  
    33.         serializedObject.Update ();
    34.  
    35.         foreach (SerializedProperty aProp in myProps)
    36.         {
    37.             EditorGUI.indentLevel = 0;
    38.             if (dArrayFoldOuts.ContainsKey(aProp.name))
    39.             {
    40.                 dArrayFoldOuts[aProp.name] = EditorGUILayout.Foldout(dArrayFoldOuts[aProp.name], aProp.name.ToUpper());
    41.  
    42.                 if (dArrayFoldOuts[aProp.name])
    43.                 {
    44.                     DrawArray (serializedObject, aProp);
    45.                 }
    46.             }
    47.             else
    48.                 EditorGUILayout.PropertyField( aProp, true );
    49.         }
    50.  
    51.         serializedObject.ApplyModifiedProperties ();
    52.     }
    53.  
    54.     void DrawArray(SerializedObject obj, SerializedProperty theArray)
    55.     {
    56.  
    57.         // get the number of items of the array
    58.         int size = theArray.arraySize;
    59.         int newSize = EditorGUILayout.IntField("Quantity:", size);
    60.         if (newSize != size)
    61.         {
    62.             theArray.arraySize = newSize;
    63.         }
    64.  
    65.         // include all the properties for the current array item
    66.         for (int i = 0; i < newSize; i++)
    67.         {
    68.             // set the indent
    69.             EditorGUI.indentLevel = 2;
    70.             EditorGUILayout.PropertyField (theArray.GetArrayElementAtIndex (i), true);
    71.         }
    72.     }
    73. }
    PropertyDrawer for Class Arrays
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Linq;
    4. using System.Collections.Generic;
    5.  
    6. [CustomPropertyDrawer (typeof(MyClassScriptName))]
    7. public class MyClassNameDrawer : PropertyDrawer {
    8.  
    9.     // Inspector Variables
    10.     public List<string> myClassVars = new List<string> ()
    11.         {
    12.             // Add variable names here
    13.             "one",
    14.             "two",
    15.             "three",
    16.             "four",
    17.             "five",
    18.             "six",
    19.             "seven",
    20.             "eight",
    21.             "firstEnumVariableName",
    22.             "secondEnumVariableName"
    23.         };
    24.  
    25.     // First Enum
    26.     public string myFirstEnum = "firstEnumVariableName";
    27.     public List<string> lstFirstEnum = new List<string>();
    28.     public Dictionary<string, List<string>> firstEnumOmits = new Dictionary<string, List<string>>()
    29.     {
    30.         // Add all your enum selections here
    31.         // string = name of enum selection
    32.         // List<string> = property fields to omit from the inspector based on the enum selection
    33.         // Only Caveat is that ALL the enum selection names need to be unique
    34.         { "EnumSel01" , new List<string> () { "three", "four" } },
    35.         { "EnumSel02" , new List<string> () { "one", "two" } }
    36.     }
    37.     // Second Enum
    38.     public string mySecondEnum = "secondEnumVariableName";
    39.     public List<string> lstSecondEnum = new List<string>();
    40.     public Dictionary<string, List<string>> secondEnumOmits = new Dictionary<string, List<string>>()
    41.     {
    42.         // Add all your enum selections here
    43.         // string = name of enum selection
    44.         // List<string> = property fields to omit from the inspector based on the enum selection
    45.         // Only Caveat is that ALL the enum selection names need to be unique
    46.         { "EnumSel01" , new List<string> () { "seven", "eight" } },
    47.         { "EnumSel02" , new List<string> () { "five", "six" } }
    48.     };
    49.  
    50.  
    51.     public override void OnGUI (Rect pos, SerializedProperty prop, GUIContent label)
    52.     {
    53.         // Follow format and add additional enums here
    54.         MyClass.MyFirstEnum firstSel = (MyClass.MyFirstEnum)prop.FindPropertyRelative(myFirstEnum).intValue;
    55.         MyClass.MySecondEnum secondSel = (MyClass.MySecondEnum)prop.FindPropertyRelative(mySecondEnum).intValue;
    56.  
    57.         // remove variables by first enum selection
    58.         lstFirstEnum = firstEnumOmits [firstSel.ToString ()];
    59.         var result = myClassVars.Except (lstFirstEnum);
    60.  
    61.         // remove variables by second enum selection
    62.         lstSecondEnum = secondEnumOmits [secondSel.ToString ()];
    63.         result = result.Except (lstSecondEnum);
    64.  
    65.         EditorGUI.BeginProperty (pos, GUIContent.none, prop);
    66.  
    67.         prop.isExpanded = EditorGUI.Foldout(pos, prop.isExpanded, label);
    68.  
    69.         if (prop.isExpanded)
    70.         {
    71.             EditorGUI.indentLevel = 3;
    72.             foreach (string str in result.ToArray())
    73.             {
    74.                 EditorGUILayout.PropertyField (prop.FindPropertyRelative (str), true);
    75.             }
    76.         }
    77.  
    78.         EditorGUI.EndProperty ();
    79.     }
    80. }
     
    matinmn87 likes this.