Switch statements in Udon Sharp
Switch statements get compiled to a jump table if the following conditions are met.met:
- The input evaluates to an integer type
(enums are a compile-time feature in U# and become integers at runtime) - The switch has at least 4 and no more than 257
valuecases - The case values are in the range 0 - 256 (inclusive)
This means that you can have at most 257 value cases and a default case.
The cases can be declared in any order..
Enums only exist at compile-time in U#. They are Int32 at runtime since the Udon VM has no support for enums.
Examples
Switch statement that compiles to a jump table:
switch (inputValue)
{
case 256:
Debug.Log("256");
break;
case 0:
Debug.Log("0");
break;
case 2:
Debug.Log("2");
break;
case 123:
Debug.Log("123");
break;
default:
Debug.Log("Default value");
break;
}
Switch statement that does not compile to a jump table and instead becomes if else if else if else ...
switch (inputValue)
{
case -1:
Debug.Log("-1");
break;
case 257:
Debug.Log("257");
break;
default:
Debug.Log("Default value");
break;
}