Creating enum using a string array

I’m trying to recreate the Tag dropdown menu.

It’s possible to create a string array that contains all the Tags in the project using

		public string[] tagsStr = UnityEditorInternal.InternalEditorUtility.tags;

So say I have a public tags enum ,

enum Tags {} 

How do I populate it with my tagsStr array? And will this need to be a custom inspector as I assume the Enum needs to be populated preenum runtime?

No, that’s not possible. An Enum need to be compiled at compile time and not at runtime. Even when it’s possible to create an enum type with the members defined by the elements of a string array, you can’t use the enum in your code since it doesn’t exist at compile time.

At runtime an enum is simply a replacement for a certain numeric / primitive value (usually int32). So an enum like this:

public enum MyEnum
{
    Cat, Dog, Snake, Cow
}

Just maps those names to integer values:

MyEnum.Cat    == 0
MyEnum.Dog    == 1
MyEnum.Snake  == 2
MyEnum.Cow    == 3

Unity’s tags are actually string based so what’s the point of creating an enum? If you want to display a dropdown for a tag selection in a custom editor, just use the EditorGUILayout.TagField or use the more general EditorGUILayout.Popup which takes the current index and a string array.

Here is youtube tutorial how to show List as Enum in inspector

https://www.youtube.com/watch?v=ThcSHbVh7xc

158744-gif.gif

I believe you could simply do this:

enum tags {

   tag0 = tagStr[0],
   tag1 = tagStr[1]

}

Just replace tag0 etc with what you want each tag to be referred to when you call tags.whatever.