Enumerations as Collections?

Is it possible to treated enumerated values as part of a collection for the purpose of populating an array with all enumerated values? For example, I’m working on a ResourceManager class that will keep an array of all possible ResourceTypes. ResourceTypes are enumerated values, such as “food”, “stone”, “wood”, etc.

What I want is to be able to tell the ResourceManager, on Awake(), to create an array of all possible resource types by using every possible enumerated value. How do I get it to create a array with the same number of elements as are in ResourceTypes? Once I get that far, I know how to add them in.

I am trying to avoid having to hard code adding each type to the array. That allows me to change the list of ResourceTypes without having to then go to the ResourceManager and add/delete the appropriate lines of code.

I can’t test it now, but I know that in some languages you can add methods to enums such as myEnum.GetMemberCount(). Or you can encode something as myEnum.Last = 5.

Yeah. I used Last. Now the problem…Can I not refer to the enuerated value by its index? If I use:

Debug.Log(myEnum[5]);

…it’ll throw an error that it does not support slicing. I can’t refer to an enum by it’s numerical value?

you can but I don’t think through array accessors, you need to use the proper function for it.

You can acces enum values like this:

import System;

enum Test {a, b, c, d}


function Awake() {
	var val : String[] = Enum.GetNames(Test);
	for (var str: String in val)
		print(str);
}

Output console:

a
b
c
d

Also, if you’re using C#, you can iterate through the values in order with the ++ operator.