Search Unity

Question Scripting dropdown to select dropdown options using enums.

Discussion in 'Scripting' started by KiwiYz, Apr 18, 2021.

  1. KiwiYz

    KiwiYz

    Joined:
    Aug 31, 2020
    Posts:
    3
    Hi, as title says I am trying to modify a class (Non mono behaviour) in order to show two dropdowns on the inspector based on enums. The first drop down selects some type, and the second dropdown selects the enum of said type, as this:

    upload_2021-4-18_18-25-48.png
    (do ignore the third dropdown please)
    Or this.
    upload_2021-4-18_18-26-12.png

    The related enums are the next
    Code (csharp):
    1.  
    2. public enum  FilterType
    3.     {
    4.         Orientation=0,
    5.         Movement=1
    6.     }
    7.  
    8.  
    9. public enum Orientation: byte
    10.     {
    11.         NE= 0b_0101,
    12.         SE= 0b_1001,
    13.         SW= 0b_1010,
    14.         NW = 0b_0110
    15.     };
    16.  
    17. public enum Movement
    18.     {
    19.         MOVING=0,
    20.         IDLE=1
    21.     }
    22.  
    Thing is, the way I am achieving this is kind of clunky and definitly not smart.
    I have a "StatusFilter" class as following:
    Code (csharp):
    1.  
    2. public class StatusFilter{
    3.        
    4.         public FilterType type;
    5.         public BaseStatus status;
    6.         public OrientationOps orientation_op;
    7.  
    8. }
    9.  
    10. [System.Serializable]
    11.     public class BaseStatus{
    12.         public Orientation orientation;
    13.         public Movement movement;
    14.         public double speed;
    15.     }
    16.  
    Finally I am using a custom drawer using the following block:
    Code (csharp):
    1.  
    2. EditorGUI.PropertyField(typeRect,property.FindPropertyRelative("type"),GUIContent.none);
    3.            
    4. if ((FilterType) property.FindPropertyRelative("type").intValue == FilterType.Orientation)
    5. {
    6.               EditorGUI.PropertyField(valueRect,property.FindPropertyRelative("status").FindPropertyRelative("orientation"),GUIContent.none);
    7.              EditorGUI.PropertyField(operationRect,property.FindPropertyRelative("orientation_op"),GUIContent.none);//ThirdDropdown
    8. }
    9.  
    10. if ((FilterType) property.FindPropertyRelative("type").intValue == FilterType.Movement)
    11. {
    12.               EditorGUI.PropertyField(valueRect,property.FindPropertyRelative("status").FindPropertyRelative("movement"),GUIContent.none);
    13. }
    14.  
    It works? yeah. But! But! Everytime I wanna add a new status field I have to:
    1 - Create the related enum (if it needs one)
    2 - Add a new variable to the BasteState class that uses said enum
    3 - Add a new value to the FilterType Enum
    4 - Manage that new filterType on the Drawer flow.
    5 - ???
    6 - Profit.

    Besides, the filter itself must have a whole Status object living inside of it which is kinda innecesary. Id like the filter to have three variables:
    1- Type of filter
    2- Value (Not a whole wooping object)
    3- Operation (the third dropdown, ignore this)

    Anyways, thats it. Does anyone have any smarter way, ideas, etc?
    I been searching the net and found using static classes with properties to work as enums, but cant translate that into the inspector dropdowns.
     

    Attached Files:

  2. Well, smarter, yes. Cheap? No. :)
    I really recommend getting Odin (asset store asset) if you can. It worth every penny. Here is the result:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public enum Orientation: byte
    4. {
    5.     NE= 0b_0101,
    6.     SE= 0b_1001,
    7.     SW= 0b_1010,
    8.     NW = 0b_0110
    9. };
    10.  
    11. [System.Serializable]
    12. public class BaseStatus{
    13.     public Orientation orientation;
    14. }
    15.  
    16. public class Test : MonoBehaviour
    17. {
    18.     public BaseStatus testField;
    19. }
    20.  
    screenshot1.png
    ps: I know you are looking for property drawer magic, I mentioned this because if you do it for yourself or for your team only, it really worth the investment, cheaper on the long run. Obviously if you do this for your asset which will be sold or published, well, it is not the proper way.