I’m looking for guidance on the best way to match a sprite item from a set of sprites in a particular project folder, let’s say Sprites, to an enum item.
I have a list where items are enum. I want foreach item in items, to add sprites matching that items enum name, say cheese or pizza, into a list
I’m using Enum.GetValues for the loop but don’t know how to pull sprites and match them.
Not even sure if this is how it’s done but I have a feeling it is?
Does LoadAssets do what I need?
**Got it… always do
Here’s some guidance to wanderers:
List<Sprite> _invSprites = new List<Sprite>();
_invSprites.AddRange(Resources.LoadAll<Sprite>("Sprites/UI/Inventory"));
for(int s = 0; s < _invSprites.Count; s++)
{
IEnumerable<Item> items = _collectedItems.Where (item => _invSprites [s].name == item.item.ToString());
if (items.Count() > 0 && _itemBoxes.Count == 0)
{
GameObject itemBox = Instantiate (_itemBox) as GameObject;
itemBox.transform.SetParent (_invHorzGrp.transform, false);
itemBox.GetComponent<SpriteRenderer> ().sprite = _invSprites [s];
itemBox.transform.localScale = new Vector3 (17, 17, 17);
_itemBoxes.Add(itemBox);
}
}
I was able to reduce code from nested for loops by using linq to find matches in enum value and sprite name
Where’s item.item in linq is the enumerated value so to match the sprite name to the enum value I just cast to string
1 Like