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;
}
}
}
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。 |
NGUI学习(二)
最新推荐文章于 2021-07-20 23:53:33 发布