Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

EnumPopup able to exclude items?

Discussion in 'Immediate Mode GUI (IMGUI)' started by sarachan, Sep 5, 2020.

  1. sarachan

    sarachan

    Joined:
    Jul 21, 2013
    Posts:
    43
    I am using EditorGUILayout.EnumPopup() and wonder if there is a way to exclude some of the enums from the popup. I understand that that the checkEnabled argument can disable some of the enums, so that they appear on the popup greyed out. Is there a way to have the disabled enum values excluded from the popup list entirely, instead of having them greyed out?

    Thanks!
    Brion
     
  2. NMJ_GD

    NMJ_GD

    Joined:
    Dec 28, 2019
    Posts:
    13
    I have same question :D
     
  3. TheRealMajorGonzo

    TheRealMajorGonzo

    Joined:
    May 12, 2018
    Posts:
    11
    There is, but I've been looking for an example, and have found none. The Unity docs list this declaration:
    Code (CSharp):
    1. public static Enum EnumPopup(GUIContent label, Enum selected, Func<Enum,bool> checkEnabled, bool includeObsolete, params GUILayoutOption[] options);
    but I can't make it work, and have found no examples anywhere.
     
  4. TheRealMajorGonzo

    TheRealMajorGonzo

    Joined:
    May 12, 2018
    Posts:
    11
    I managed to figure it out piecing together bits from numerous sites. I'm putting it here because I could not find a single complete example anywhere.
    I have a Weapon class which has:

    Code (CSharp):
    1. public enum FireModes { Semi = 1, Burst = 2, Full = 4 }
    and in my editor, for each weapon, you can select one or more FireModes using:
    Code (CSharp):
    1. weapon.fireModes = (FireModes)EditorGUILayout.EnumFlagsField("Fire Modes", weapon.fireModes);
    The editor auto generated a None and Everything option.
    I then allow selection of the Starting/Initial FireMode using:
    Code (CSharp):
    1. GUIContent label = new GUIContent("Starting Fire Mode");
    2. weapon.fireMode = (FireModes)EditorGUILayout.EnumPopup(label, weapon.fireMode, DisplayFireMode, true );
    This shows all 3 options, but dims out any FireModes not selected above. BTW, if you include None = 0 in your enum declaration, then it's an option here, so I deleted mine since Unity autogenerated the option anyway.

    I declared my method as follows:
    Code (CSharp):
    1. bool DisplayFireMode(Enum enumValue){
    2.    bool displayMode = false;
    3.    if (weapon.fireModes.HasFlag(enumValue)){
    4.        displayMode = true;
    5.    }
    6.    return displayMode;
    7. }
    One thing that stung me for a while. I had to include:
    Code (CSharp):
    1. Using System;
    at the top of the class;
     
    Last edited: Jun 30, 2021
    iwkio likes this.