Use enum hidden value as something other than int? (C#)
I realize that doing something like:
enum direction { Forward, Back, Left, Right };
is assigning Forward = 0, Back = 1, Left = 2, Right = 3. It's also possible to use different ints such as
enum direction { Forward = 10, Back = 20, Left = 5, Right = 162 };
.
.
My question is, is it possible (and if so how?) to make these values a type of data other than int? I want to do something like
enum<Vector3> direction { ForwardLeft = new Vector3(-1, 0, 1), Special = new Vector 3(0.65f, 0.2f, 1) };
Answer by Dave-Carlile · Oct 22, 2015 at 06:59 PM
Enums are integer types by definition, so no. You can make static read only properties in a class though...
public static class Direction
{
public static readonly Vector3 ForwardLeft = new Vector3(-1, 0, 1);
// or
public static Vector3 ForwardLeft { get { return new Vector3(-1, 0, 1); } }
}
I'll do this and make it a nested class so it essentially works the same way. Thanks!
Answer by Eric5h5 · Oct 22, 2015 at 07:07 PM
The type of enums can be changed like this:
public enum Foo : byte {Forward, Back}
However only integer-like types (byte, uint, long, etc.) can be used, nothing like Vector3.
Good to know! Is that a limitation specifically for Unity and/or C#, or is this the way enums work pretty much across the board?