admin 管理员组文章数量: 1086864
NGUI,背包拖拽,以及随机克隆图片知识点
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/
public class Knapsackltem : UIDragDropItem{
int count = 1;
public UILabel numTxt;//预设体上面的文本
public void Add()//添加物品的数量方法
{
count++;//一直累加
numTxt.text = count.ToString();//赋值给文本
}
protected override void OnDragDropRelease(GameObject surface)//OnDragDropRelease:拖放,丢弃,释放
{
base.OnDragDropRelease(surface);//调用父类的方法
print(surface);//碰撞的物体
if (surface.CompareTag("GeZi"))
{
//物品居中
transform.parent = surface.transform;//把靴子放到格子里面
transform.localPosition = Vector3.zero;//把靴子放在格子的正中间
}
else if (surface.CompareTag("WuPin"))
{
//物品交换
Transform parent = surface.transform.parent;//护腕的父对象
surface.transform.parent = transform.parent;//把护腕放入靴子下面
surface.transform.localPosition = Vector3.zero;//护腕居中
transform.parent = parent;//把靴子放入护腕下面
transform.localPosition = Vector3.zero;//靴子居中
}
else
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/
public class MyKnapsack : MonoBehaviour {
public GameObject[] cells;//格子
public string[] names;//物品名称
public GameObject item;//当前物品
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
PickUp();
}
}
void PickUp()
{
int index = Random.Range(0, names.Length);//位置
string name = names[index];//随机物品图片名称
for (int i = 0; i < cells.Length; i++)//随机格子
{
if (cells[i].transform.childCount == 0)//格子里面没有物品
{
GameObject go = NGUITools.AddChild(cells[i], item);//克隆一个物品放到格子里面
go.transform.GetComponent<UISprite>().spriteName = name;//更换物品名称
go.transform.localPosition = Vector3.zero;//物品居中
break;
}
else//格子里面有物品
{
//找到预设体上面的图片
UISprite sprite = cells[i].transform.GetChild(0).GetComponent<UISprite>();
if (sprite.spriteName.Equals(name))
{
sprite.GetComponent<Knapsackltem>().Add();//调用预设体上面的脚本
break;
}
}
}
}
}
本文标签: NGUI 背包拖拽 以及随机克隆图片知识点
版权声明:本文标题:NGUI,背包拖拽,以及随机克隆图片知识点 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1693887977a247615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论