NGUI背包系統(tǒng)的制作##
效果展示
一琅锻、背包系統(tǒng)的搭建##
背包格子的搭建
二、格子里面物品(預(yù)制物的制作)##
- 格子里面的物體需要實(shí)現(xiàn)可以在格子里面拖拽向胡,如果是空的格子可以替換位子浅浮,如果有另外一個(gè)物品需要實(shí)現(xiàn)交換物品,如果是拉在間隙部分捷枯,或者背包系統(tǒng)以外就回歸到原來位子滚秩。
2.數(shù)量Label的位置,可以坐在物體上淮捆,也可以放在格子上郁油。
預(yù)制物的制作
預(yù)制物上的腳本:
public class DragTest : UIDragDropItem {
public UISprite sprite;
public UILabel label;
int num = 1;
public void AddCount(int number=1)
{
num += number;
label.text = num + "";
}
protected override void OnDragDropRelease(GameObject surface)
{
base.OnDragDropRelease(surface);
//print(surface);
if (surface.tag == "cell"||surface.tag==null)
{
this.transform.parent = surface.transform;
transform.localPosition = Vector3.zero;
}
else if (surface.tag == "UIRoot" || surface.tag == null)
{
transform.localPosition = Vector3.zero;
}
else//必須給物體添加tag值不然的話本股。。桐腌。拖拽到window外面去的話就會(huì)報(bào)錯(cuò)拄显。。案站。躬审。
{
Transform temp = surface.transform.parent;
surface.transform.parent = transform.parent;
transform.parent = temp;
surface.transform.localPosition = Vector3.zero;
transform.localPosition = Vector3.zero;
}
}
}
三、對(duì)格子及其物品的管理##
對(duì)格子的管理
腳本控制:
public class KnapsackAdd : MonoBehaviour
{
public GameObject[] cells;
public GameObject item;
public string[] equpimentsName;//承儲(chǔ)物品的名字
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
PickUp();
}
}
void PickUp()
{
int random = Random.Range(0, equpimentsName.Length);
string name = equpimentsName[random];
bool isFind = false;
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].transform.childCount > 0)
{
DragTest testDrage = cells[i].transform.GetComponentInChildren<DragTest>();
if (testDrage.sprite.spriteName == name)
{
testDrage.AddCount();
isFind = true;
break;
}
}
}
if (isFind == false)
{
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].transform.childCount==0)
{
GameObject go = NGUITools.AddChild(cells[i], item);
go.GetComponent<UISprite>().spriteName = name;
go.transform.localPosition = Vector3.zero;
break;
}
}
}
}
}
四蟆盐、總結(jié)難點(diǎn)##
1.進(jìn)行交互的物體承边,必須添加boxcollider;
2.實(shí)現(xiàn)在不同的腳本上獲取需要的腳本的信息石挂;