Search Unity

  1. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  2. Unity 2017.2 beta is now available for download.
    Dismiss Notice
  3. Unity 2017.1 is now released.
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  6. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice

Custom Editor with enum problem!

Discussion in 'Scripting' started by luckie12, Aug 13, 2017.

  1. luckie12

    luckie12

    Joined:
    Aug 17, 2014
    Posts:
    140
    Hi, i have this 'item' inspector so i can make new items. The problem however is,
    i have 2 enum types in there:

    [​IMG]

    One is called Item Type, you can choose from CARD or ITEM in here, when you choose CARD
    it shows you the fields below the separator, but whenever i change Item Type to ITEM again, the fields (Extra Damage, Extra Health, Extra Stamina) never go away....
    They just stay where they are...

    [​IMG]
    As you can see they still inherit the Card Type 'Mixed'... I have no idea how i would change this,

    this is my script for the inspector:


    Code (CSharp):
    1. [CustomEditor(typeof(Item))]
    2. public class CardScriptEditor : Editor
    3. {
    4.     public override void OnInspectorGUI()
    5.     {
    6.  
    7.  
    8.         Item myItem = (Item)target;
    9.  
    10.         EditorGUILayout.LabelField("Item name", myItem.name, EditorStyles.boldLabel);
    11.         myItem.name = EditorGUILayout.TextField("Item Name", myItem.name);
    12.         myItem.itemTypes = (Item.itemType)EditorGUILayout.EnumPopup("Item Type", myItem.itemTypes);
    13.         string selectedType = myItem.itemTypes.ToString();
    14.  
    15.         myItem.icon = (Sprite)EditorGUILayout.ObjectField("Sprite", myItem.icon, typeof(Sprite), allowSceneObjects: true);
    16.         GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) }); // USE AS SEPARATOR
    17.         switch (selectedType)
    18.         {
    19.             case "Item":
    20.  
    21.                 // myItem.extraSlot = EditorGUILayout.IntField("Item Slot(s)", myItem.extraSlot); //Placeholder
    22.  
    23.                 break;
    24.             case "Card":
    25.  
    26.                 myItem.cardTypes = (Item.cardType)EditorGUILayout.EnumPopup("Card Type", myItem.cardTypes);
    27.                 string cardTypes = myItem.cardTypes.ToString();
    28.                 break;
    29.  
    30.             default:
    31.                 break;
    32.         }
    33.  
    34.  
    35.         switch (myItem.cardTypes)
    36.         {
    37.             case Item.cardType.Inventory:
    38.  
    39.                 myItem.extraSlot = EditorGUILayout.IntField("Item Slot(s)", myItem.extraSlot);
    40.  
    41.                 break;
    42.             case Item.cardType.Damage:
    43.  
    44.                 myItem.extraDamage = EditorGUILayout.IntField("Extra Damage", myItem.extraDamage);
    45.  
    46.                 break;
    47.             case Item.cardType.Health:
    48.                 myItem.extraHealth = EditorGUILayout.IntField("Extra Health", myItem.extraHealth);
    49.                 break;
    50.  
    51.             case Item.cardType.Stamina:
    52.                 myItem.extraStamina = EditorGUILayout.IntField("Extra Stamina", myItem.extraStamina);
    53.                 break;
    54.  
    55.             case Item.cardType.Mixed:
    56.                 myItem.extraDamage = EditorGUILayout.IntField("Extra Damage", myItem.extraDamage);
    57.                 myItem.extraHealth = EditorGUILayout.IntField("Extra Health", myItem.extraHealth);
    58.                 myItem.extraStamina = EditorGUILayout.IntField("Extra Stamina", myItem.extraStamina);
    59.                 break;
    60.  
    61.             default:
    62.                 break;
    63.         }
    64.  
    65.  
    66.  
    67.         /*
    68.         EditorGUILayout.LabelField("Warning", EditorStyles.boldLabel);
    69.         EditorGUILayout.HelpBox("Card expansion still work in progress!", MessageType.Warning);
    70.  
    71.         EditorGUILayout.HelpBox("Some features are not working correctly yet!", MessageType.Error);
    72.      
    73.         */
    74.         //base.OnInspectorGUI();
    75.     }
    76. }
    Thank you so much for every reply!

    -Luc
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    195
    You could do this by wrapping the cardTypes switch in an if statement, or moving it into the previous switch. But you could simplify it all a bit, like this:

    Code (CSharp):
    1. Item myItem = (Item)target;
    2.  
    3. myItem.name = EditorGUILayout.TextField("Item Name", myItem.name);
    4. myItem.itemTypes = (Item.itemType)EditorGUILayout.EnumPopup("Item Type", myItem.itemTypes);
    5.  
    6. myItem.icon = (Sprite)EditorGUILayout.ObjectField("Sprite", myItem.icon, typeof(Sprite), allowSceneObjects: true);
    7. GUILayout.Box("", new GUILayoutOption[] { GUILayout.ExpandWidth(true), GUILayout.Height(1) }); // USE AS SEPARATOR
    8.  
    9.  
    10. if(myItem.itemTypes == Item.itemType.Item)
    11. {
    12.     // myItem.extraSlot = EditorGUILayout.IntField("Item Slot(s)", myItem.extraSlot); //Placeholder
    13. }
    14. else
    15. {
    16.     myItem.cardTypes = (Item.cardType)EditorGUILayout.EnumPopup("Card Type", myItem.cardTypes);
    17.  
    18.     switch (myItem.cardTypes)
    19.     {
    20.     case Item.cardType.Inventory:
    21.  
    22.         myItem.extraSlot = EditorGUILayout.IntField("Item Slot(s)", myItem.extraSlot);
    23.         break;
    24.     case Item.cardType.Damage:
    25.  
    26.         myItem.extraDamage = EditorGUILayout.IntField("Extra Damage", myItem.extraDamage);
    27.         break;
    28.     case Item.cardType.Health:
    29.         myItem.extraHealth = EditorGUILayout.IntField("Extra Health", myItem.extraHealth);
    30.         break;
    31.  
    32.     case Item.cardType.Stamina:
    33.         myItem.extraStamina = EditorGUILayout.IntField("Extra Stamina", myItem.extraStamina);
    34.         break;
    35.  
    36.     case Item.cardType.Mixed:
    37.         myItem.extraDamage = EditorGUILayout.IntField("Extra Damage", myItem.extraDamage);
    38.         myItem.extraHealth = EditorGUILayout.IntField("Extra Health", myItem.extraHealth);
    39.         myItem.extraStamina = EditorGUILayout.IntField("Extra Stamina", myItem.extraStamina);
    40.         break;
    41.  
    42.     default:
    43.         break;
    44.     }
    45. }