- Home /
Assigned enum value changes when a new type is added to the enum
I'd like to be able to add a new type in the mid of the enum, without changing all the assigned enum values in the editor.
To simplify I can show this example. I start creating the enum and assigning it a value.
Then I decide to add a type inside the enum. But doing so it changes some of the assigned values in the editor.
The simplest solution would be just to add the new types always at the end of the list. But I've long enums, and if I keep doing that the code will start looking pretty messy.
Is there any way to make sure the assigned enums do not change also if I add a new type at the start or in the mid of the enum type list?
Answer by Dray · Nov 30, 2017 at 09:13 AM
According to your comment on @haruna9x answer I suggest you to check out the ISerializationCallbackReceiver interface.
With that you can "translate" fields into other fields before they get serialized (usually for making unserializable fields serializable). You could for example map your enums to string values in the OnBeforeSerialize function and then later back to the enums in the OnAfterDeserialize.
edit: that would require another 'lookup' variable though where you map the strings to the enum/int values and vice versa. Let me know if you need a sample
Answer by haruna9x · Nov 17, 2017 at 09:56 AM
You just assign it a fixed value. By default, the first enum value is 0, and then incremented by 1.
Unity does not know you changed the enum class and it deserialize based on the value of the enum.
public enum TestEnum
{
One=1,
Two=2,
TwoBis=3,
Three=4,
}
I had already tried assigning ints, but this didn't solved my issue. It works only if I do the following: public enum TestEnum { One=1, Two=2, TwoBis=4, Three=3, }
But doing this TwoBis would appear at the end of the list if I select it in the editor, and not at the third position.
In the future I could create the next enum with some gaps, so I can fill them later, like { One=1, Two=5, Three=10, } So I might add TwoBis = 7 But this doesn't solve my current problem.
Good. So how did you try it?
Step 1:
public enum TestEnums
{
One = 1,
Two = 2,
Three = 4,
}
And I selected Three from the Inspector window.
Step 2:
public enum TestEnums
{
One = 1,
Two = 2,
TwoBis = 3,
Three = 4,
}
And it's still Three in the Inspector.
Yes. I confirm this solve the problem of my future enums and could solve also situation with non assigned enum.
Alas in my present enums are already assigned to ints and I do not have gaps between them. (Sorry, this was not explicit in my starting question).
I agree that your solution would work if they weren't assigned.
$$anonymous$$y current situation is:
public enum TestEnums { One = 1, Two = 2, Three = 3, }
Your answer
Follow this Question
Related Questions
Default Editor : Enum as flags ? 8 Answers
Initialising List array for use in a custom Editor 1 Answer
DropDownList with string array in Editor Inspector 5 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers