Unity3d 检查克隆是否等于原始对象
我正在开发一个库存系统,我有一个手工制作系统,现在我想创建一个类似RPG的手工制作队列,你可以在一个物品上点击3次,如果你有足够的资源,它将为你制作3次,我开始制作,但由于某种原因,最初的手工制作系统坏了,下面是当你想要制作一些东西时会发生的事情 当你点击制作配方时:Unity3d 检查克隆是否等于原始对象,unity3d,if-statement,foreach,Unity3d,If Statement,Foreach,我正在开发一个库存系统,我有一个手工制作系统,现在我想创建一个类似RPG的手工制作队列,你可以在一个物品上点击3次,如果你有足够的资源,它将为你制作3次,我开始制作,但由于某种原因,最初的手工制作系统坏了,下面是当你想要制作一些东西时会发生的事情 当你点击制作配方时: //override Use Function public override void Use() { //call AddCraftingItem from Inventory Inventory.insta
//override Use Function
public override void Use() {
//call AddCraftingItem from Inventory
Inventory.instance.AddCraftingItem(this);
}
添加工艺项目:
public void AddCraftingItem(CraftinRecipe newCraftingRecipe) {
CraftingQueue.Enqueue(newCraftingRecipe);
if(!isCrafting) {
isCrafting = true;
//start crafting
StartCoroutine(CraftItem());
}
}
工艺项目:
private IEnumerator CraftItem() {
//check if queue is empty
if(CraftingQueue.Count == 0) {
Debug.Log("Queue is empty");
isCrafting = false;
yield break;
}
CraftinRecipe currentRecipe = CraftingQueue.Dequeue();
//check if we have enough resources
//this is where things broke
//CraftItem return false
if(!currentRecipe.CraftItem()) {
//Debug.Log("You don't have enough Resources");
CraftingQueue.Clear();
isCrafting = false;
yield break;
}
Debug.Log("TEST");
yield return new WaitForSeconds(currentRecipe.craftTime * 1.1f);
//add item to inventory
AddItem(currentRecipe.result);
Debug.Log("Added " + currentRecipe.result + " to inventory");
//check if continue crafting
if(CraftingQueue.Count > 0) {
yield return StartCoroutine(CraftItem());
} else {
isCrafting = false;
}
}
public bool CraftItem() {
if(!CanCraft()) {
//Debug.Log("CanCraft returned false");
return false;
} else {
Debug.Log("CanCraft returned true");
}
RemoveIngredientsFromInventory();
//start crafting
ParentCraftingSlot.StartCrafting();
return true;
}
工艺项目:
private IEnumerator CraftItem() {
//check if queue is empty
if(CraftingQueue.Count == 0) {
Debug.Log("Queue is empty");
isCrafting = false;
yield break;
}
CraftinRecipe currentRecipe = CraftingQueue.Dequeue();
//check if we have enough resources
//this is where things broke
//CraftItem return false
if(!currentRecipe.CraftItem()) {
//Debug.Log("You don't have enough Resources");
CraftingQueue.Clear();
isCrafting = false;
yield break;
}
Debug.Log("TEST");
yield return new WaitForSeconds(currentRecipe.craftTime * 1.1f);
//add item to inventory
AddItem(currentRecipe.result);
Debug.Log("Added " + currentRecipe.result + " to inventory");
//check if continue crafting
if(CraftingQueue.Count > 0) {
yield return StartCoroutine(CraftItem());
} else {
isCrafting = false;
}
}
public bool CraftItem() {
if(!CanCraft()) {
//Debug.Log("CanCraft returned false");
return false;
} else {
Debug.Log("CanCraft returned true");
}
RemoveIngredientsFromInventory();
//start crafting
ParentCraftingSlot.StartCrafting();
return true;
}
CanCraft功能:
//function that return bool if we can craft the Item
public bool CanCraft() {
//loop trough each ingredient type of ingredient in ingredient
//(don't worry bro i don't understand what i just said too)
foreach(Ingredient ingredient in ingredients) {
//bool if we Contains current Ingredients
//here this function return false
bool ContainsCurrentIngredient = Inventory.instance.ContainsItem(ingredient.item,
ingredient.amount);
//if ContainsCurrentIngredient is false
if(!ContainsCurrentIngredient) {
//we return false
return false;
}
}
//return true
return true;
}
ContaineItems(这是东西损坏的地方):
问题是,在InventoryItemList中有一个项目的克隆,假设我有两个熨斗,我需要一个熨斗来制作一些东西,我猜问题是我是克隆的,所以它不等于该项目,这就是为什么它不向itemCounter添加1,然后脚本认为我们没有足够的资源来制作一些东西,我试着搜索并询问我的一些朋友如何检查克隆是否与物品相同,我正在尝试修复这件事大约2天,我很想听到任何关于如何修复它或如何使我的代码更优化的答案,感谢您阅读而不是直接检查
i==item
在item
类中有一些属性,其中包含如下项目类型的信息
public class item
{
public enum Type
{
IronBar, GoldBar //etc
}
public Type itemType;
}
然后在ContainItems()
中,可以使用
public bool ContainsItem(Item.Type itemType, int amount){
// other code
foreach(Item i in InventoryItemList) {
if(i.itemType == itemType) {
ItemCounter++;
}
// other code
}