Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Help with Enum & Casting

Discussion in 'Scripting' started by globenorth, Feb 9, 2017.

  1. globenorth

    globenorth

    Joined:
    Sep 7, 2010
    Posts:
    217
    Hey everyone!
    Been a bit, hoping someone can help me out here...

    Code (CSharp):
    1.     void PopulateDropdown(Dropdown target, Enum targetList){
    2.         //CLEAR : Default Data
    3.         target.options.Clear();
    4.         //POPULATE : Dropdown
    5.         for(int target_ID = 0; target_ID < Enum.GetValues(targetList.GetType()).Length; target_ID++){
    6.             //WORKS - Works but not as desired
    7.             //target.options.Add(new Dropdown.OptionData(((VehicleManager.WheelData.WheelType)target_ID).ToString()));
    8.             //NEED TO CORRECT - Once does same as above line will be very happy :)
    9.             target.options.Add(new Dropdown.OptionData(((targetList.GetType())target_ID).ToString()));
    10.         }
    11.         //REFRESH : Dropdown Data
    12.         target.RefreshShownValue();
    13.     }
    14.  
    If I could just get the last line within the for loop working life would be peachy! The line above it works just fine...so I'm at a loss.

    Any suggestions welcome, I've built a fairly complex script and this would greatly help me shave down on it quite a bit.

    Apologies for formatting, no wifi atm, posting via phone...
     
  2. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    For one, it's probably not wise to assume that the enum values start at zero and have no gaps, so you really shouldn't be working with target_ID as an int anyway. Keep the array returned by Enum.GetValues(), pull values directly from that, and use Enum.GetName() to convert the values to strings.
    Code (CSharp):
    1.     void PopulateDropdown(Dropdown target, Enum targetList){
    2.         target.options.Clear();
    3.         var enumType = targetList.GetType();
    4.         var enumValues = Enum.GetValues(enumType);
    5.         foreach (var value in enumValues)
    6.         {
    7.             target.options.Add(new Dropdown.OptionData(Enum.GetName(enumType, value));
    8.         }
    9.         target.RefreshShownValue();
    10.     }
    Alternatively, just use Enum.GetNames() to get an array of strings directly, if you don't have any other use for the values.
    Code (CSharp):
    1.     void PopulateDropdown(Dropdown target, Enum targetList){
    2.         target.options.Clear();
    3.         var enumType = targetList.GetType();
    4.         var enumNames = Enum.GetNames(enumType);
    5.         foreach (var name in enumNames)
    6.         {
    7.             target.options.Add(new Dropdown.OptionData(name);
    8.         }
    9.         target.RefreshShownValue();
    10.     }
    If you really want to get fancy, since you don't actually need the value of targetList, just its type, you can make the function generic, specifying the type of the enum instead of some arbitrary value.

    Code (CSharp):
    1.     void PopulateDropdown<TEnum>(Dropdown target){
    2.         target.options.Clear();
    3.         var enumType = typeof(TEnum);
    4.         var enumNames = Enum.GetNames(enumType);
    5.         foreach (var name in enumNames)
    6.         {
    7.             target.options.Add(new Dropdown.OptionData(name);
    8.         }
    9.         target.RefreshShownValue();
    10.     }
    You'd call the above with something like this:
    Code (CSharp):
    1. PopulateDropdown<VehicleManager.WheelData.WheelType>(dropdown);
     
    globenorth and djfunkey like this.
  3. globenorth

    globenorth

    Joined:
    Sep 7, 2010
    Posts:
    217
    Hey thanks! All of your suggestions work, so, if you don't mind...which would you suggest I use and why (vs the others)? Also...what's a TEnum, I can't seem to find any documentation on this...

    Currently I'm going with your last suggestion, tho I don't fully understand it. Luckily this is a GUI menu n functionality vs my understanding isn't really all that important haha

    Anyway many thanks! I can get back to important stuff now :)
     
  4. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In above example TEnum is a generic parameter: https://msdn.microsoft.com/en-us/library/512aeb7t.aspx

    It allows you to specificy the type of the enum as follows:
    Code (CSharp):
    1. public enum Sample {
    2.     This,
    3.     Is,
    4.     An,
    5.     Example
    6. }
    7.  
    8. void SampleGeneric<TEnum>(Dropdown target)
    9. {
    10.     target.options.Clear();
    11.         var enumType = typeof(TEnum);
    12.         var enumNames = Enum.GetNames(enumType);
    13.         foreach (var name in enumNames)
    14.         {
    15.             target.options.Add(new Dropdown.OptionData(name);
    16.         }
    17.         target.RefreshShownValue();
    18. }
    19.  
    20. void Start)
    21. {
    22.     var dropdown = GetComponent<Dropdown>();
    23.    
    24.     // Here is specifiy that this function call uses the enum 'Sample'
    25.     SampleGeneric<Sample>(dropdown);
    26. }
     
    AndyGainey and globenorth like this.
  5. globenorth

    globenorth

    Joined:
    Sep 7, 2010
    Posts:
    217
    Much appreciated, again thank you guys!