NGUI 背包暂时完成
2015-12-15 23:59
519 查看
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;(居中
}
}
}
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;(居中
}
}
}
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- [软件资讯]Unity已支持将3D游戏导出成Flash
- Unity 武器拖尾效果
- unity常见问题之20题
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- (Endless)Scroll View(画卷滚动视图)
- Unity 预编译宏
- Unity AssetDatabase类
- VS2013中 命名空间“UnityEngine”中不存在类型或命名空间名称“UI”。是否缺少程序集引用?
- unity 实现炸弹人放炸弹后只进不出的一种方法
- Unity3D占用内存太大的解决方法
- 【Unity】安卓项目使用带有资源Jar包SDK的打包方法