Public enum used in diferent classes (better way)
What I'm looking is not an answer how can it be done but can it be done better. So in my project I have public enum called item that is used in multiple lists to determine which sprite should be applied.
Code:
public enum spriteID
{
rock_pickaxe = 0,
iron_pickaxe,
silver_pickaxe,
diamond_pickaxe,
normal_gun,
hunter_gun,
nuclear_gun,
size
}
public Sprite[] sprite = new Sprite[(int)spriteID.size-1];
Whenever I want to apply sprite to gameObject I will take it from sprite array( public Sprite[] sprite ).
I have Shop script which creates gameObjects with sprites that are determined by spriteID enum and applied.
I have Inventory class with gameObjects with sprites that are determined by spriteID enum and applied.
3.)I have Player class with one GameObject representing weapon that will have one of the sprites apllied.
This works and I do not need to create Sprites for every class or public sprites than drag my sprites (numberOfSprites x numOfScripts) times. BUT what my question is is there a better way. The reason that I'm using enums is that if I have list it will be much easier to acces specific item by myList[normal_gun]
than myList[3]
for example.
Thanks in advance.
Answer by Jessespike · Feb 23, 2016 at 05:46 PM
The reason that I'm using enums is that if I have list it will be much easier to acces specific item by myList[normal_gun]than myList[3] for example.
An alternative would be to use a Dictionary since you can assign keys and values. Whether it's better or not is debatable.
Dictionary<string, Sprite> myDictionary = new DictionaryBase<string, Sprite>();
//myDictionary["normal_gun"]
Your answer
![](https://koobas.hobune.stream/wayback/20220612065319im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sprites appear broken in android build but not editor? 0 Answers
Stop animation at any frame? or have two idle states? 0 Answers
Sprite Baking Studio 0 Answers
Question for @Mavina , What Program? 1 Answer
Programs for making 2d graphic spites 0 Answers