所以我试图将一些不同项目的列表分类到允许堆叠的库存槽列表中,并且每个槽只有 1 个带有计数的项目精灵
So I'm trying to sort a list of a few distinct item into an inventory slot list that ALLOWS stacking and only 1 item sprite per slot with a count
我暂时通过使用一堆列表首先将它们分类来完成此操作,但我知道这可以通过嵌套的 for 循环来完成。
TIA
public void AddToInventory(List<FoodResource> food)
{
for (int s = 0; s < slots.Length; s++)
{
for (int f = 0; f < food.Count; f++)
{
}
}
}
public class UISlot : MonoBehaviour
{
public FoodResource item;
public Image slotImage;
public Sprite icon;
public Text itemCount;
}
public class FoodResource : MonoBehaviour
{
public FoodResourceType foodType;
public Sprite sprite;
public float resourceValue;
public string uniqueName;
}
鉴于您正在传递可能已经或可能尚未将实例分配给 UISlot 的食品列表,因此最好将食品循环作为您的外循环。这样它会查看食物,然后查看所有插槽以查找匹配的食物类型,如果存在则将其插入,然后移动到下一个食物。例如:
for (int f = 0; f < foods.Count; f++) {
bool wasInserted = false;
for (int s = 0; s < slots.Length; s++) {
if (slots[s].item != null && slots[s].item.foodType == foods[f].foodType)
{
// Increase the number in your itemCount Text
wasInserted = true;
break; // It's inserted so move on to the next food item
}
}
// If the food wasn't inserted into any slot, find the first empty slot and insert it
if (!wasInserted) {
for (int s = 0; s < slots.Length; s++) {
if (slots[s].item == null) {
// Add the food to the inventory slot
break;
}
}
}
}
上面假设你的插槽和你的食物都被实例化了。
有一种使用 Linq 执行此操作的更简单方法,它可能会更快。大致如下:
using System.Linq;
/*
other stuff
*/
List<UISlot> slotsList = slots.ToList();
for (FoodResource foodResource in food) {
UISlot selectedSlot = slotsList.FirstOrDefault(s => s.item != null && s.item.foodType == foodResource.foodType);
if (selectedSlot != null) {
// Increment the itemCount
} else {
selectedSlot = slotsList.FirstOrDefault(s => s.item == null);
// Add item to empty slot
}
}
或类似的东西。
在不知道 how/when 你已经实例化项目的情况下,我认为这应该非常接近你所需要的。
我暂时通过使用一堆列表首先将它们分类来完成此操作,但我知道这可以通过嵌套的 for 循环来完成。
TIA
public void AddToInventory(List<FoodResource> food)
{
for (int s = 0; s < slots.Length; s++)
{
for (int f = 0; f < food.Count; f++)
{
}
}
}
public class UISlot : MonoBehaviour
{
public FoodResource item;
public Image slotImage;
public Sprite icon;
public Text itemCount;
}
public class FoodResource : MonoBehaviour
{
public FoodResourceType foodType;
public Sprite sprite;
public float resourceValue;
public string uniqueName;
}
鉴于您正在传递可能已经或可能尚未将实例分配给 UISlot 的食品列表,因此最好将食品循环作为您的外循环。这样它会查看食物,然后查看所有插槽以查找匹配的食物类型,如果存在则将其插入,然后移动到下一个食物。例如:
for (int f = 0; f < foods.Count; f++) {
bool wasInserted = false;
for (int s = 0; s < slots.Length; s++) {
if (slots[s].item != null && slots[s].item.foodType == foods[f].foodType)
{
// Increase the number in your itemCount Text
wasInserted = true;
break; // It's inserted so move on to the next food item
}
}
// If the food wasn't inserted into any slot, find the first empty slot and insert it
if (!wasInserted) {
for (int s = 0; s < slots.Length; s++) {
if (slots[s].item == null) {
// Add the food to the inventory slot
break;
}
}
}
}
上面假设你的插槽和你的食物都被实例化了。
有一种使用 Linq 执行此操作的更简单方法,它可能会更快。大致如下:
using System.Linq;
/*
other stuff
*/
List<UISlot> slotsList = slots.ToList();
for (FoodResource foodResource in food) {
UISlot selectedSlot = slotsList.FirstOrDefault(s => s.item != null && s.item.foodType == foodResource.foodType);
if (selectedSlot != null) {
// Increment the itemCount
} else {
selectedSlot = slotsList.FirstOrDefault(s => s.item == null);
// Add item to empty slot
}
}
或类似的东西。
在不知道 how/when 你已经实例化项目的情况下,我认为这应该非常接近你所需要的。