0

This works for creating a field that has a drop down box with all available options.

   [System.Serializable]
    private enum researchTypes
    {
        PHYSICS, BIOLOGY, ENGINEERING, ALL, NONE
    }

This does not work when I try to do the same thing, except now with the intention of being able to list several enumerated items.

    [System.Serializable]
    private enum worldCardTagTypes
    {
        NONE, ROCKY, ICE, MINIMAL_GRAVITY, GAS_GIANT //
    }


    [SerializeField] List<worldCardTagTypes> worldCardTags = new List<worldCardTagTypes>;

My goal is to be able to edit a list of tags. If there is something wrong with how I asked this question, please feel free to comment on that, too.

1 Answers1

1

Why not rather use a Flags instead

[Flags]
private enum researchTypes
{
    // Important that the values are 2^X so they aren't overlapping on a bits level 
    PHYSICS = 1,
    BIOLOGY = 2,
    ENGINEERING = 4
}

[SerializeField] private researchTypes worldCardTags;

For this Unity will show a drop-down where you can select multiple items.

Afaik by default it will include also a Nothing (==0) and Everything (==bitwise "sum"/combination of all) option.

You then use bitwise and shift operators to combine, invert and check for certain values.

E.g. for combining or appending multiple values use bitwise OR

var physicsAndBiology = researchTypes.PHYSICS | researchTypes.BIOLOGY;

For checking if a certain flag is contained either bitwise AND

var hasPhysics = physicsAndBiology & researchTypes.PHYSICS == researchTypes.PHYSICS;

or also

 var hasPhysics = physicsAndBiology.HasFlag(researchTypes.PHYSICS);

If you want to check if any of some given flags is set you can use e.g.

var hasPhysicsOrBiologyOrBoth = worldCardTags & researchTypes.PHYSICS & researchTypes.BIOLOGY != 0;

or if you want to check if both flags are set then

var hasPhysicsAndBiology = worldCardTags.HasFlag(researchTypes.PHYSICS | researchTypes.BIOLOGY);
derHugo
  • 83,094
  • 9
  • 75
  • 115