Attributes: Adding extra data on to Enums

Overview

In the video below I show you how to add and use extra data on Enums



This is all done by using attributes on to Enums



Files

TitleAttribute.cs
using System;

namespace eWolf.CodeExamples.EnumnAttributes
{
    [AttributeUsage(AttributeTargets.All)]
    public class TitleAttribute : Attribute
    {
        public TitleAttribute(string title)
        {
            Title = title;
        }

        public string Title { get; set; }
    }
}


EnumHelper.cs
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using UnityEngine;

namespace eWolf.CodeExamples.EnumnAttributes
{
    public class EnumHelper : MonoBehaviour
    {
        public static string GetEnumDescription(System.Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

            if (attributes != null && attributes.Any())
            {
                return attributes.First().Description;
            }

            return value.ToString();
        }

        public static string GetEnumTitle(System.Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            TitleAttribute[] attributes =
                    fi.GetCustomAttributes(typeof(TitleAttribute), false) as TitleAttribute[];

            if (attributes != null && attributes.Any())
            {
                return attributes.First().Title;
            }

            return value.ToString();
        }
    }
}