Backpack System (III)
10, modify the item script, complete the item number plus 1 function
Public Uisprite Sprite;
Public UILabel label;
Private int count = 1;
Public void Addcount (int number)
{
Count + = number;
Label.text = count + "";
}
11, modify the grid script, complete the current game item name and we picked up the name of the game items, the number of items plus 1; the name of the current game item is different from the name of the game item we picked up, add the item "Dog planing learning net" to the new lattice.
bool flag = false;
for (int i = 0; i < cells.) Length; i++)
{
if (Cells[i].transform.childcount > 0) //There are items in the current grid
{
knapsack kn = cells[i]. getcomponentinchildren<knapsack> ();
//The name of the current game item is the same as the name of the game item we found .
if (Kn.sprite.spriteName = = name)
{
Flag = true;
Kn. Addcount (1);
break;
}
}
}
//The name of the current game item is not the same as the name of the game item we picked up
if (flag = = false) {
for (int i = 0; i < cells.) Length; i++)
{
if (Cells[i].transform.childcount = = 0) //There is no backpack in the current grid
{
gameobject go = nguitools. AddChild (Cells[i], item); //Put the newly-created backpack inside the grid
Go. getcomponent<uisprite> (). spritename = name; //Add a name for the newly generated backpack
Go.transform.localPosition = Vector3. Zero; //Center The Backpack
break;
}
}
}
Unity3d Game Development Backpack System (III)