x


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

more ▼

asked Sep 28 '10 at 06:15 PM

dekarguy gravatar image

dekarguy
3 1 1 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Sep 28 '10 at 06:57 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

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.

Sep 28 '10 at 07:04 PM dekarguy

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 LayerMask 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.

Sep 28 '10 at 10:09 PM skovacs1

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.

Sep 30 '10 at 05:01 PM dekarguy
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1675
x824
x240
x130
x18

asked: Sep 28 '10 at 06:15 PM

Seen: 1926 times

Last Updated: Sep 28 '10 at 06:15 PM