NGUI学习(二)

 NGUI制作背包系统:

1.制作Prefab
在上面的制作中,先是建立一张sprite当作背包的背景,作为knapsack_bg,然后在下面添加sprite,浅色的图片当作存放物品的格子cells,此处有16格 ,为格子添加Box Collider,方便拖动时的触发碰撞检测,实现互动。
制作knapsack_item的prefab,添加一张sprite,添加物品图片,添加Box Collider,添加脚本KnapsackItem,该脚本继承
UIDragDropItem的,复写该类中的protected override void OnDragDropRelease (GameObject surface)函数,这个函数的功能是得到释放物品时所处下方的对象信息(surface)。然后进行物品拖拽的一个处理。并在物品右下角设置一个label显示物品数量。
protected override void OnDragDropRelease (GameObject surface)
{
base.OnDragDropRelease (surface);

//如果拖拽到的格子是空的则直接将物品移过去
//如果有其他物体则交换位置
if (surface.tag == "Cell") {//cell这个需要自己在tag里面设置该对象名称
this.transform.parent = surface.transform; //将物品放到当前格子下
this.transform.localPosition = Vector3.zero; //设置物品位置居中
}
else if(surface.tag == "KnapsackItem") {
Transform parent = surface.transform.parent;
surface.transform.parent = this.transform.parent;
surface.transform.localPosition = Vector3.zero;

this.transform.parent = parent;
this.transform.localPosition = Vector3.zero;
}
}  
这样子prefab制作完成。
2.脚本控制拾到物品后对背包内信息的更新
    1〉拾到背包中不存在的物品
,直接在背包中的空闲格中显示
    2〉拾到
背包中存在的物品,在原先物品上的数量加1
控制的主要算法:
for (int i = 0; i < cells.Length; i++) {//判断当前格子有没有物品
if(cells[i].transform.childCount > 0){
//如果有
KnapsackItem item = cells[i].GetComponentInChildren<KnapsackItem>();
//判断当前物品的名字是否和见到的物品的名字一样
if(item.sprite.spriteName == name){
isFind = true;
item.AddCount(1);
break;
}
}
                }

直接在knapsack_bg上挂载脚本 knapsack,(此处模拟生成拾到物品)
using UnityEngine;
using System.Collections;

public class Knapsack : MonoBehaviour {

public GameObject[] cells;    //给背包中所有格子赋值
public string[] equipmentsName;    //物品图片资源的名字
public GameObject item;    //item的prefab

void Update(){
if (Input.GetKeyDown (KeyCode.X)) {
Pickup();
}
}

public void Pickup(){
int index = Random.Range (0,equipmentsName.Length);
string name = equipmentsName [index];
bool isFind = false;

for (int i = 0; i < cells.Length; i++) {//判断当前格子有没有物品
if(cells[i].transform.childCount > 0){
//如果有
KnapsackItem item = cells[i].GetComponentInChildren<KnapsackItem>();
//判断当前物品的名字是否和见到的物品的名字一样
if(item.sprite.spriteName == name){
isFind = true;
item.AddCount(1);
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;
}
}
}
}
}


knapsack_item 上挂载 脚本 KnapsackItem:
using UnityEngine;
using System.Collections;

public class KnapsackItem : UIDragDropItem {

public UISprite sprite;    //用来控制该物体图片的更换,也就是用来实现不同物品的显示
public UILabel label;    //用来控制显示物品数量label显示
private int count = 1;

public void AddCount(int number = 1){    //拾到物品后物品数量增加
count += number;
label.text = count + "";
}

protected override void OnDragDropRelease (GameObject surface)
{
base.OnDragDropRelease (surface);

//如果拖拽到的格子是空的则直接将物品移过去
//如果有其他物体则交换位置
if (surface.tag == "Cell") {
this.transform.parent = surface.transform; //将物品放到当前格子下
this.transform.localPosition = Vector3.zero; //设置物品位置居中
}
else if(surface.tag == "KnapsackItem") {
Transform parent = surface.transform.parent;
surface.transform.parent = this.transform.parent;
surface.transform.localPosition = Vector3.zero;

this.transform.parent = parent;
this.transform.localPosition = Vector3.zero;
}
}
}  

最后还需要实现整个背包的一个拖拽,这个直接让背包的背景knapsack添加Box Collider,添加脚本Drag Object,指定Target为此knapsack_bg的UISprite。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值