Switch statements in Udon Sharp
Switch statements get compiled to a jump table if the following conditions are met.
1.
- The input evaluates to an integer type (enums are a compile-time feature in U# and become integers at runtime)
2. - The switch has at least 4
value casesand no more than 257 value cases3. - The case values are in the range 0 - 256 (inclusive)
This means you can have at most 257 explicit value cases and a default case.
The cases can be declared in any order.
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;
}