I have a simple enum
value type for 3 camera modes. I know how to print out each value (e.g. Debug.Log(OrbitStyle.Smooth)
or Debug.Log(OrbitStyle.Smooth.ToString())
) but in order to print them all out, I thought I had to write a function for it.
My first question is: Is this the only way to do so, or is there a function for looping through enum
s?
My second question is: Why does my Unity3D
program crashes when I add =
to include all values, while incrementing/decrementing? The program below prints out Smooth, Step
and then Fade, Step
but I use <=
or >=
to include the least and highest values as well, but it always crashes. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOribit : MonoBehaviour
{
enum OrbitStyle
{
Smooth,
Step,
Fade
}
void Start()
{
for ( OrbitStyle e1 = OrbitStyle.Smooth; e1 < OrbitStyle.Fade; e1 = IncrementEnum(e1) )
{
Debug.Log(e1);
}
for ( OrbitStyle e2 = OrbitStyle.Fade; e2 > OrbitStyle.Smooth; e2 = DecrementEnum(e2) )
{
Debug.Log(e2);
}
}
static OrbitStyle IncrementEnum(OrbitStyle e)
{
if (e == OrbitStyle.Fade)
return e;
else
return e + 1;
}
static OrbitStyle DecrementEnum(OrbitStyle e)
{
if (e == OrbitStyle.Smooth)
return e;
else
return e - 1;
}
}