Flag itself doesn’t do much, and can be a bit misleading as it starts numbering at 0. The key is that you each value is a power of 2 so you can use bitwise operators.
Here’s a bit of a sample:
using UnityEngine;
using System.Collections;
public class FruitEnumTest : MonoBehaviour {
// Use this for initialization
void Start () {
Test(FruitEnum.Apple);
Test(FruitEnum.Banana);
Test(FruitEnum.Orange);
Test(FruitEnum.Pear);
Test(FruitEnum.AppleOrPear);
Test(FruitEnum.AnythingButAnOrange); // Probably not the best way to use it.
}
void Test(FruitEnum fruit) {
Debug.Log("Testing " + fruit);
if ((fruit FruitEnum.AppleOrPear) == fruit) Debug.Log("It's an Apple or Pear");
if ((fruit FruitEnum.AppleOrPear) == FruitEnum.None) Debug.Log("It's not an Apple or Pear");
if ((fruit FruitEnum.Orange) == FruitEnum.None) Debug.Log("It's not an Orange");
if ((fruit FruitEnum.AllFruit) == fruit) Debug.Log("It's definitely something");
Debug.Log("-----------------");
}
}
public enum FruitEnum{
None = 0,
Apple = 1<<0,
Pear = 1<<1,
Orange = 1<<2,
Banana = 1<<3,
AllFruit = ~0,
AppleOrPear = Apple | Pear,
AnythingButAnOrange = AllFruit ^ Orange
}
void ExampleFunction2()
{
//It would be better to make this a class member instead of a local variable
List<ExampleEnum> stateCheck = new List<ExampleEnum>() {enum1, enum2, enum3};
if(!stateCheck.Contains(currentState))
{
...
}
}