我已经创建了c#database(在内存对象列表中),然后在启动时将哪些代码注入Unity检查器。
之后我创建了一个脚本,该脚本应该将项目添加到您的库存中。出于某种原因,此代码适用于我的一个脚本,其中自动将3个项目添加到void Start()
中的广告资源。
但它不想使用buttonClick
函数(它在必须添加项目的情况下工作正常)。我认为它与库存大小有关,现在让我们说3。
当我使用inventory.Add(database.Items[16]);
我的脚本是这样的:
public class crateOpener : MonoBehaviour
{
float money;
float wandererCrateValue = 0.60f;
float pioneerCrateValue = 1.15f;
float gamescomCrateValue = 1.91f;
float survivorCrateValue = 0.64f;
public List<Item> inventory = new List<Item>();
public List<Item> slots = new List<Item>();
private ItemDatabase database;
public Text moneyText;
public void openCrate(string crateName)
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
double doubleMoney = Convert.ToDouble(System.IO.File.ReadAllText("money.txt"));
float money = ToSingle(doubleMoney);
if (crateName == "Wanderer")
{
if (money < wandererCrateValue)
{
Debug.Log("You don't have enough moeny!");
}
else
{
openWandererCrate();
money -= wandererCrateValue;
System.IO.File.WriteAllText("money.txt", money.ToString());
moneyText.text = money.ToString() + "$";
inventory.Add(database.Items[1]);
inventory.Add(database.Items[2]);
Debug.Log("You opened Wanerer Crate");
Debug.Log("Money left: " + money);
}
}
else if (crateName == "Pioneer")
{
if (money < pioneerCrateValue)
{
Debug.Log("You don't have enough moeny!");
}
else
{
openPioneerCrate();
money -= pioneerCrateValue;
System.IO.File.WriteAllText("money.txt", money.ToString());
moneyText.text = money.ToString() + "$";
Debug.Log("You opened Wanerer Crate");
Debug.Log("Money left: " + money);
}
}
else if (crateName == "Gamescom")
{
if (money < gamescomCrateValue)
{
Debug.Log("You don't have enough moeny!");
}
else
{
openGamescomCrate();
money -= gamescomCrateValue;
System.IO.File.WriteAllText("money.txt", money.ToString());
moneyText.text = money.ToString() + "$";
Debug.Log("You opened Wanerer Crate");
Debug.Log("Money left: " + money);
}
}
else if (crateName == "Survivor")
{
if (money < survivorCrateValue)
{
Debug.Log("You don't have enough moeny!");
}
else
{
openSurvivorCrate();
money -= survivorCrateValue;
System.IO.File.WriteAllText("money.txt", money.ToString());
moneyText.text = money.ToString() + "$";
Debug.Log("You opened Wanerer Crate");
Debug.Log("Money left: " + money);
}
}
}
void openWandererCrate()
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
//addMoney(wandererCrateValue);
}
void openPioneerCrate()
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
//addMoney(pioneerCrateValue);
}
void openGamescomCrate()
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
//addMoney(gamescomCrateValue);
}
void openSurvivorCrate()
{
database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
//addMoney(survivorCrateValue);
}
public static float DoubleToFloat(double dValue)
{
if (float.IsPositiveInfinity(Convert.ToSingle(dValue)))
{
return float.MaxValue;
}
if (float.IsNegativeInfinity(Convert.ToSingle(dValue)))
{
return float.MinValue;
}
return Convert.ToSingle(dValue);
}
public static float ToSingle(double value)
{
return (float)value;
}
}
这是数据库
public class ItemDatabase : MonoBehaviour
{
public List<Item> Items = new List<Item>();
//Edit missing ItemType3 colors... Or wrong colors... And add missing colors to database...
void Start()
{
//Wanderer Crate
Items.Add(new Item("Bloody Combat Pants", 0, 0.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Blue Hi-top Trainers", 1, 2.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Blue));
Items.Add(new Item("Boots (Punk)", 2, 0.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Blue));
Items.Add(new Item("Cargo Pants (Beige)", 3, 0.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Cargo Pants (Blue)", 4, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Cargo Pants (Khaki)", 5, 0.06f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Dirty Tank-top (White)", 6, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Floral Shirt", 7, 10.19f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Glasses (Punk)", 8, 0.37f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Glasses, Item.ItemType3.Blue));
Items.Add(new Item("Hotpants (Blue)", 9, 138.12f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Long Sleeved T-shirt (Striped)", 10, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Long-sleeved T-shirt (Black)", 11, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Long-sleeved T-shirt (Light Blue)", 12, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Mandarin Jacket (Blue)", 13, 18.60f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Jacket, Item.ItemType3.Blue));
Items.Add(new Item("Padded Jacket(Urban)", 14, 1.77f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Jacket, Item.ItemType3.Blue));
Items.Add(new Item("Pleated Mini-skirt (Blue)", 15, 170.97f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("Ballistic Mask", 16, 19.02f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Mask, Item.ItemType3.Blue));
Items.Add(new Item("Protective Glasses", 17, 45.43f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Glasses, Item.ItemType3.Blue));
Items.Add(new Item("School Shirt (Open)", 18, 17.14f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("School Shoes", 19, 17.14f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Blue));
Items.Add(new Item("Working Boots", 20, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shoes, Item.ItemType3.Blue));
Items.Add(new Item("Slacks (Blue)", 21, 4.20f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Pants, Item.ItemType3.Blue));
Items.Add(new Item("T-shirt (Red)", 22, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("T-shirt (Striped)", 23, 0.04f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
Items.Add(new Item("Tank - top(Charcoal)", 24, 0.09f, Item.ItemType1.Wanderer_Crate, Item.ItemType2.Shirt, Item.ItemType3.Blue));
}
}