#Tags type/enum

1 messages · Page 1 of 1 (latest)

limber hatch
#

Hey, not a fan of having raw strings in my game code so was going to make a type for unity tags. I'm not sure how best to do this; as Enums cannot take string values - any recommended ways? I'd also like to be able to use it as a dropdown type for serialised parameters inside the unity editor if possible, to further reduce chances of my own user error breaking things

verbal jolt
#

I once wrote a script that turns the tags defined in the editor into an enum (or rather, a bit mask).
My approach used a monobehaviour to contain the tag, but the auto generation script could be expanded to translate enum entries back to strings as well.

limber hatch
#

Interesting, thanks.
I'll take a look into this. Were you doing it for similar reasons as keeping things strongly typed?
My thoughts for turning it back into a string is just to ensure .CompareTag(Tags.tagA) still works as expected in code. Thanks again dude, will see what I can come up with!

verbal jolt
tiny reef
#

Hello, if I understand you, you can use enum string. For example YourEnum.EnumText.ToString(). In result u get string enum. If u need int you can get this (int)YourEnum.EnumText

verbal jolt
#

As for doing turning the enum back into a string, you won't be able to reliably do that with ToString, as the enum value names could have changed (since tags don't have to adhere to the same rules as C# member names).