Hey guys,
So I just learnt about these switch statements. I’m wondering if I can have it function like an If statement whereby I can say " If THIS && THAT are selected"
so where it says switch (ColorState), can I have something like Switch (ColorState && UsageState)?
{
switch ( ColorState )
{
case ColorStates.Mana:; case
Manaliquidpriv.sprite = Resources.Load(“PotionSprites/3”);
break;
case ColorStates.Health:
Manaliquidpriv.sprite = Resources.Load<Sprite>("PotionSprites/3_red");
break;
case ColorStates.Energy:
Manaliquidpriv.sprite = Resources.Load<Sprite>("PotionSprites/3_red_small");
break;
case ColorStates.Spell:
Manaliquidpriv.sprite = Resources.Load<Sprite>("PotionSprites/3");
break;
}
Yes. If you add the [FlagsAttribute] to your enum, you can “add”, or OR, enums together to produce combinations. For example:
[FlagsAttribute]
public enum ColorStates
{
None = 0,
Mana = 1,
Health = 2,
Energy = 4,
Spell = 8
}
Notice that the values are incremented by powers of 2. ( 2^0, 2^1, 2^2 etc.)
The you can use the switch statement like such:
switch (ColorState)
{
case ColorStates.Mana | ColorStates.Health:
// do something for Mana and Health ...
break;
case ColorStates.Energy | ColorStates.Spell:
// do something else for Energy and Spell ...
break;
case ColorStates.Mana:
// do something just for Mana ...
break;
case ColorStates.Health:
// do something else just for Health ...
break;
}
Enums are explained in detail at MSDN.