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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to display an enum in UI with spaces

Discussion in 'Scripting' started by Hotpots, Mar 26, 2017.

  1. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    I have tried accessing the System.ComponentModel.DataAnnotations namespace, but it seems that it does not exist.

    I have tried the following without the specified namespace and I get and error:
    Code (CSharp):
    1. public enum QuestState
    2. {
    3.     Available,
    4.     NotAvailable, [Description("Not Available")]
    5.     InProgress,
    6.     Finished,
    7.     Completed,
    8.     NA
    9. }
    Error:
    upload_2017-3-26_15-46-8.png

    Any help is appreciated! :)

    I assume when I perform enum.ToString(); after specifying the description of the enum, it will be displayed with a space as specified in Description?
     
  2. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Is there any reason that the System.ComponentModel.DataAnnotations namespace does not exist?
     
  3. Hotpots

    Hotpots

    Joined:
    Apr 9, 2015
    Posts:
    143
    Still cant find a fix :(
     
  4. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I just did this and it worked just fine:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.ComponentModel;
    4.  
    5. public class TestScript01 : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         Debug.Log(GetPrettyName(QuestState.NotAvailable));
    11.         Debug.Log(GetPrettyName(QuestState.Completed));
    12.     }
    13.  
    14.  
    15.     public static string GetPrettyName(System.Enum e)
    16.     {
    17.         var nm = e.ToString();
    18.         var tp = e.GetType();
    19.         var field = tp.GetField(nm);
    20.         var attrib = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
    21.  
    22.         if (attrib != null)
    23.             return attrib.Description;
    24.         else
    25.             return nm;
    26.     }
    27.  
    28.     [Description("Quest State")]
    29.     public enum QuestState
    30.     {
    31.         [Description("Available")]
    32.         Available,
    33.         [Description("Not Available")]
    34.         NotAvailable,
    35.         [Description("In Progress")]
    36.         InProgress,
    37.         Finished,
    38.         Completed,
    39.         NA
    40.     }
    41.  
    42. }
    43.