1.下一步做到如何在3D的游戏中做到呼出 NGUI的菜单
2.贴上背包代码
using UnityEngine;
using System.Collections;
public class Knapsack : MonoBehaviour {
public GameObject[] cells; (定义格子数
public string[] equipment;(定义物品数
public GameObject item;(物品
public void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
packup();
}
public void packup()
{
int index = Random.Range(0, equipment.Length);
string name = equipment[index];(获取物品名字的方法
bool isFind = false;
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].transform.childCount > 0)
{
MyDraDropitem item =cells[i].GetComponentInChildren<MyDraDropitem>();(获取子物体也就是item是否存在
if(item .sprite.spriteName==name)
{
isFind = true;
item.addcount(1);(+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;
}
}
}
}
}
调换物体
using UnityEngine;
using System.Collections;
public class MyDraDropitem : UIDragDropItem {
public UISprite sprite;
public UILabel lable;
private int count=1;
public void addcount(int num=1)
{
count += num;
lable.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=="item")
{
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;(居中
}
}
}