Search Unity

How do you assign Enum options to a dropdown.

Discussion in 'Scripting' started by BIO_K, Jul 3, 2022.

  1. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
    Like the question asks... I have a UI script with all the Dropdowns and another which does the Enums case switches (its really long). So I want the dropdown to be populate with the Enums. make sense?

    Code (CSharp):
    1.  
    2. ChangeClothes clothes;
    3.  
    4.     public Dropdown clothingType;
    5.  
    6.     void Awake()
    7.     {
    8.         ClearAll();
    9.  
    10.         clothingType.enabled = true;
    11. //Basicly I want clothingType.options = clothes.EnumOptions
    12.         clothingType.onValueChanged.AddListener(delegate
    13.         {
    14.             OnClothingTypeChange();
    15.         });
    16.     }
    17.  
    18.     void OnClothingTypeChange()
    19.     {
    20. //Whatever Should Happen
    21.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748
    You can use
    Enum.GetValues()
    to get the enums as a collection and iterate them.

    But keep this in mind:

    Enums enums are bad in Unity3D if you intend them to be serialized:

    https://forum.unity.com/threads/bes...if-not-do-something-else.972093/#post-6323361

    https://forum.unity.com/threads/unity-card-game-structure.1006826/#post-6529526

    It is much better to use ScriptableObjects for many enumerative uses. You can even define additional associated data with each one of them, and drag them into other parts of your game (scenes, prefabs, other ScriptableObjects) however you like. References remain rock solid even if you rename them, reorder them, reorganize them, etc. They are always connected via the meta file GUID.

    Collections / groups of ScriptableObjects can also be loaded en-masse with calls such as
    Resources.LoadAll<T>().
     
  3. BIO_K

    BIO_K

    Joined:
    Feb 3, 2016
    Posts:
    23
    I promise I will learn that, I was waiting for ECS and all... But yea still getting issues with new Input System. but for now can you show me how to iterate the values to to a drop down?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,748