- Home /
Insert enum breaking existing inspector values
Enums are great, but unfortunately when used as fields that can be changed in the inspector they are completely dependent on the order of the enum in the code. I find it curious that Unity does not have an internal level of indirection for enum values from one script build to the next, so that you can insert new values anywhere into an enum without breaking all the existing fields that have values set.
Edit: It's also worth it to note that I would like the enum values to remain consecutive as the integer values will be bit packed tightly with other values during the runtime.
Is there a C# attribute or something that would help me here? Some other kind of field? Any way to make these more useful in the Unity inspector? I could totally write a custom inspector to do this, but writing a custom inspector for every single component that uses an enum doesn't seem like an efficient use of my time.
Here's some simple code just to illustrate exactly what I'm talking about
public enum Foo
{
A,
B,
C
}
Then you change it to
public enum Foo
{
A,
NewValue,
B,
C
}
Thanks.
It might be possible to write a PropertyDrawer for your specific enum. Word of warning, though, I've been building up a giant generic Editor-GUI utility class and trying to genericize enums is some of the most hair-pullingly frustrating stuff. I dunno if that'll present a problem with a PropertyDrawer, but you'll probably need a separate PropertyDrawer for every enum you want to behave the right way, if you do go this route.
I didn't know about property drawers ... those are incredibly cool since it would save me from having to write a while editor, i can just make an editor for the single field.
Using the property drawer, the enum can be backed by a separate string field which is parsed by the runtime as the object is created so it is ready for use. I'm not wild about having to have an additional string field for everything using an enum, but the ref counted strings of C# make the additional space required not too terrible, even if used in a prefab that is instanced many many times.
Answer by robertbu · Aug 05, 2013 at 06:13 PM
You can explicitly set the enum values:
public enum Foo {
Dog = 1,
Snake = 3,
Cat = 7,
Bird = 5
}
Thanks, this is a perfectly legit solution given the requirements I laid out. Of course, what I neglected to say is that I want to keep the enum values packed closely together as the integer value of it will be bit packed into another data structure during the runtime. I'll update the question ... but if others out there don't need consecutive values this will work for you.
Awesome trick with enums. I have a list that is going to get huuuuge. If every alphabetical addition broke all editor references, I would have had to come up with a new solution.
Answer by colinday · Aug 06, 2013 at 01:54 AM
Also adding Louis's answer here, I can't seem to vote up or mark a comment:
It might be possible to write a PropertyDrawer for your specific enum. Word of warning, though, I've been building up a giant generic Editor-GUI utility class and trying to genericize enums is some of the most hair-pullingly frustrating stuff. I dunno if that'll present a problem with a PropertyDrawer, but you'll probably need a separate PropertyDrawer for every enum you want to behave the right way, if you do go this route. -Louis