- Home /
Flag structure variable in unity editor
Is there a construct in JavaScript or Unity which behaves like a typical flag structure and have an associated editor.
For example, in C++ I would have:
enum { FLAG1 = 1 << 0, FLAG2 = 1 << 1, FLAG2 = 1 << 2, FLAG3 = 1 << 3 };
u8 myFlags = FLAG1 | FLAG3;
myFlags |= FLAG2;
And I would imagine an editor would work like a list of booleans, or an enum dropdown with checks for which values are present.
Thanks for the help
Answer by skovacs1 · Sep 28, 2010 at 06:57 PM
An enum works just like that.
enum foo {bar, bash, bang};
var blah : foo;
In the inspector, the variable blah has a drop-down of values from the enum foo.
To allow for multiple simultaneous selections is a bit trickier. That requires something that will generate an IntPopUp. One of the things that gets in the way is that .Net doesn't implicitly treat ints as enum types (and vice versa) and another is that Unity's inspector doesn't seem to do anything special with the System.FlagsAttribute as near as I can tell.
I'm still digging to find a way to serialize something in generic other than a LayerMask which generates an IntPopUp. As it stands, the only way that I can see is to create a CustomEditor for scripts and a custom class of integer or enum for which your CustomEditor will generate an IntPopUp. Please see these scripts for an idea of how to get started on that.
The case I am looking for is where in the inspector I can select both bar and bash and if I query foo I can test if bar or bash are selected.
Ah! A type which allows for multiple simultaneous selections in the editor from a list of preset values which acts as a bitmask. I didn't get that from the question - Sorry. The only type I know of that behaves in this way is the Layer$$anonymous$$ask struct and I get the feeling that there's some editor scripted magic going on to enable that (with the mixed, nothing and everything options). I'll add what information I've found on this to the answer.
Thanks for the info. I resorted to creating a new class containing a bunch of bools, which I then added as var's for my behaviors. Achieves representing flags, just not quite the interface I was looking for. I'll have to look into the IntPopUp stuff and CustomEditors a bit more.