I am currently using Unity 2020.3.20f1. I’m having an issue where an enum I’ve created, which can store multiple values, and is set using an EnumFlagsField, returns -1 when all items are selected, instead of the maximum value like it used to in earlier versions of Unity.
Here is the enum:
public enum ManualEditorDirection { None = 0, North = 1 << 0, East = 1 << 1, South = 1 << 2, West = 1 << 3}
I used to have no issues using this. I would set the values in the inspector and it would always return a value between 0 and 15. Now however, when all values are selected, it returns -1. I’ve made a hacky workaround (Setting the value to 15 if it is less than 0) for now, but this definitely doesn’t seem correct.
To recreate the issue, create a new project, create a class with the enum above, create an editor script for that class that allows you to set it using the EnumFlagsField, and then use a Debug.Log to send “(int)enumName” to the console. You’ll see that for any combination of values it will return the correct total, but if you select them all it will be -1.
I’m trying to cache various stuff in arrays where each combination of the enum flags numerically determines the index.
cachedStuff[(int)enumValue] = …
so obv. -1 is not a good result.
ofc. the solution suggested by Kurt-Dekker is certainly fast enough executionwise (compared to the if) but it is nevertheless annoying to have to patch like that all the time.
This seems like a bug in the editor where they assume -1 (i.e. all bits) are valid when selecting “Everything”.
Should be pretty easy to sum up the values of all the existing flags.
I know what you’re saying, however at no point has a bitmask be limited to just the values that may be defined in the enum. The MaskField deliberately sets the value to -1 (or 0xFFFFFFFF when unsigned) when everything is selected.
Well, yes, kind of. But you must not sum them up but combine them with a bitwise or.
Note in the past there was no enum bit field support at all. We had to use the MaskField. However it didn’t care, hasn’t support for the actual bit value of an item. So it assumes an increasing bit index for each item. I’ve written a wrapper back then which did support all combinations of the enum values and would handle mixed values as well. Even back then selecting “everything” means it returns -1. However you can add an “ALL” value to your enum at the end to cover that case. My field actually iterates through all values and reconstructs the actual value each time. Back then there was no other way.
Yes, the behaviour of the EnumFlagsField’s everything is a bit strange. It even returns -1 when you turn on your bits one by one. Once all are selected it jumps to -1 ^^. The issue with this is, there are probably some Unity internal UI stuf that may rely on this behaviour, so changing the behaviour would be quite difficult. So it’s unlikely that this would happen.
Yeah, well regardless of the source being enums or otherwise it would be preferable to not get -1 when selecting Everything.
Actuallly for flags, which is obv the case here, the result is the same ^^
Yes I actually have an ‘All’ option which is the combination of all flags available. I guess a workaround would be if the ‘Everything’ options could be opted out and that it did not go for -1 when manually selecting all options.
Exactly Or the enum contains “grouping” members. One C# default enum I came across again recently is NumberStyles. Any, Currency, Float, HexNumber, Integer and Number are all compound entries.