Previous words
In many types of games we often use the backpack, the use of the backpack to set the corresponding character attributes, the more the backpack will also make the game to add a lot of color.
So how do you write a backpack system in Unity3d game development? Because we have developed the Ngui plug-in, so we can simply write code settings to achieve a simple backpack function.
Preparatory work
- Create a new project Packagedemo
- Import the Ngui package (the latest version of the 3.9.8 version of the genuine 89 knives)
- Get the picture footage ready and pack it into an atlas using Ngui
Backpack
Backpack lattice
Items
Simple backpack feature first step, add Ngui control, use footage
The project directory and the scene list are as follows:
It is important to note that:
- The package, cell, and obj are Ngui sprite controls
- cell01-cell09 all set the tag named "Cell".
- UI Root set tag named "Uiroot"
- The box collider components are added to the package, cell, and obj
The second step is to add the Ngui script UI Drag Object to the package
Add the script so that the package can be dragged and moved.
The third step is to add a custom script to obj Mydraganddrop
Add a script directly in the properties panel of obj MyDragAndDrop.cs component
To open and edit a script:
- Make Mydraganddrop inherit Uidragdropitem
- Overriding the Ondragdroprelease method
The Mydraganddrop script code after the change is as follows:
usingUnityengine; Public classMydraganddrop:uidragdropitem {//This method is used to get the object that is colliding when the dragged object is released and dragged. //So we need to add box Collider to both cell and obj in front of us protected Override void Ondragdroprelease(Gameobject surface) {Base. Ondragdroprelease (surface);//Determine if the tag of the object getting the collision is a cell if(Surface.tag = ="Cell") {//Put obj in the sub-list of the colliding cellTransform.parent = Surface.transform;//Set obj relative to cell coordinates of 0Transform.localposition = Vector3.zero; }Else{//If the object being collided with is not a cell, put obj in the object child list of tag uiroot, releasing obj and the cell parent-child relationshipTransform.parent = Gameobject.findgameobjectwithtag ("Uiroot"). Transform; } }}
Step three, test
Here is a simple example to complete.
As you can see, when I drag obj to the edge of each cell, obj automatically moves to the collision cell, similar to the adsorption function.
The above simple example realizes the function that the object is loaded by the knapsack, and can realize the knapsack and the character attribute association by detecting the cell's sub-object by adding corresponding logic.
Expand 1. Inventory exchange between items in the backpack
Set the tag to "obj" for all items and set the parent object of the Exchange item if the OBJECT tag is "obj" when the item is dragged and released.
2. Pick up items in your backpack
Generally pick up the items to the backpack it is sorted from left to right, from top to bottom filled with empty inventory.
The implementation is to load all the inventory objects into an array of gameobject.
Through the loop to determine whether the inventory has sub-objects, that is, to determine whether the inventory value is more than 0 sub-list
If it is less than or equal to 0, the item's parent is set to the inventory and then set to 0 relative to the parent object's coordinates.
There are a lot of people to realize the free play source
Link: Http://pan.baidu.com/s/1dF0kSlN Password: ZAMK
Unity3d using Ngui for simple backpack functions