EnumLengthGenerator is a source generator solution to generate a static class that contains enum length.
To install, paste the following URL into Unity's Package Manager:
- Open Package Manager.
- Click the + button.
- Select "Add package from git URL...".
- Enter:
https://github.com/hoangtongvu/EnumLengthGenerator.git?path=/Assets/EnumLengthGenerator- Make sure all of the code that's going to use EnumLengthGenerator is under an .asmdef and references the
EnumLengthGenerator.asmdef
Put the [GenerateEnumLength] attribute on any enum that you want to generate the length for:
namespace Core.Harvest
{
[GenerateEnumLength]
public enum HarvesteeType : byte
{
None = 0,
Tree = 1,
ResourcePit = 2,
}
}The source generator will generate a static class <EnumName>_Length (with the same namespace and underlying type as the original enum):
// HarvesteeType_Length.g.cs
// <auto-generated />
namespace Core.Harvest
{
public static class HarvesteeType_Length
{
public const byte Value = 3;
}
}