- Home /
Using C# Enums to Control an Animator
So I have a GameObject with a C# script that has several enums which control the behavior of my object in game. For example:
public enum MovementState
{
Idle,
Hanging,
Falling,
Swapping,
Snapping
}
public enum State
{
Inactive,
Active,
Matching
}
I also have an animator on the same object which has a few different animations that I want to be able to control using the enums above. Should I make an int parameter for each enum in my Animator? Or should I control the animator through a switch statement in my C# code? Is there a way to nicely work with multiple enumerated states in Animator?
Answer by fafase · Aug 05, 2014 at 05:32 AM
"Should I make an int parameter for each enum?"
Your enum members are already integers. The first is given the value 0 and the following are increasing by 1. It is possible to control the value by giving an explicit int but I don't think you need that here.
"Is there a way to nicely work with multiple enumerated states in Animator?"
Mixing different enums is not possible but you can mix enum of the same type by marking them as flag.
http://www.dotnetperls.com/enum-flags
In this case you would have both enums in one and you could have none (being just a 0 enum), one or many states at once.
I know that enums are ints. I also do not want to use one single enum statement for multiple object states. What I want to know is whether or not there's a good way to work with enumerated types inside my animator class or if it is better to use a switch statement inside of my C# script which manually sets which state should be playing.
If I try to send my enum value directly to my Animator parameter as an int, I can no longer see the enumeration 'name' that I have defined for it. This will make it harder for artists to work with my system as it requires specific knowledge of the enumeration order.
You can easily go back and forth between int and enum. Passing the int to the animator, you lose the name but you can get it back on the other side:
print((EnumType)intValue);
you could come up with an editor script to show the Enum value converted from int.