当前位置:网站首页>NGUI,背包拖拽,以及随机克隆图片知识点
NGUI,背包拖拽,以及随机克隆图片知识点
2022-06-11 10:20:00 【ying1228475251】
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;
}
}
}
}
}
边栏推荐
- 【高并发】关于线程池,蚂蚁金服面试官问了我这些内容!!
- Secret behind the chart | explanation of technical indicators: tangqi'an channel
- WordPress网站备份
- Wsarecv: an existing connection was forcefully closed by the remote host
- 【DBSCAN】DBSCAN实例
- Drink at night, 50 classic SQL questions, really fragrant~
- Servlet 的初次部署
- Knowledge drop - personality analysis - four types of method
- Use of JMeter (simulating high concurrency)
- 选择DC-DC开关电源控制器的实战过程
猜你喜欢
随机推荐
面试复习手写题--函数截流与抖动
Dynamically render data and carousels
Mysql--索引
Start jar
rpc的正确打开方式|读懂Go原生net/rpc包
Practical process of selecting DC-DC switching power supply controller
微信小程序之点餐系统附源码
Empire CMS imitates DIY handmade website source code of craft activity /92kaifa imitates self-adaptive mobile phone version template of craft activity
How much do you know about the functions and functions of Rexroth solenoid directional valve
知识点滴 - 性格分析-四类法
[Clickhouse column] user initialization of new library role
为什么DDRx的电源设计时需要VTT电源
Browserfetcher class for getting started with puppeter
Pagoda panel backup and recovery data
Ugui mouse click diffusion UI effect
Detail measurement of DC-DC and PDN with network analyzer
How much do you know about software compatibility testing? How to select a software compatibility testing organization?
[audio and video] Introduction to SEI
接口调优的大致思路
MySQL基础篇常用约束总结上篇









