unity简单的背包制作

您所在的位置:网站首页 简单的皮包制作 unity简单的背包制作

unity简单的背包制作

2024-07-05 10:26:12| 来源: 网络整理| 查看: 265

背包系统基本功能:添加物品、删除物品、拖拽物品、交换物品位置、排列物品等等。

首先在unity将背包的UI界面制作完成,如图:

 Text是用来显示当前格子物品的数量。

然后将需要生成的物品UI制作成预制物,如图所示:

 接下来上代码

1、添加物品

添加物品的时候需要注意的是(1)当前格子是否为空(2)当前格子是否有跟需要添加相同的物体(3)格子为空的时候需要按照空格子的顺序添加物体(4)当前格子有需要添加的物体时,只需要数量++

/// /// 添加物品 /// /// void LoadObjs(int objnum) { switch (objnum) { case 101: //red GameObject objred = GameObject.Find("red"); if (objred != null) { int num = int.Parse(objred.transform.parent.GetChild(0).GetComponent().text); num++; objred.transform.parent.GetChild(0).GetComponent().text = num.ToString(); return; } for (int i = 0; i < bagBakcGround.transform.childCount; i++) { if (bagBakcGround.transform.GetChild(i).childCount 1) //如果格子不为空 { if(Mathf.Abs(Vector3.Distance(transform.position, bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).position)) < 2.5f) { //----更换物品数量--- string targetNum = partar.GetChild(0).GetComponent().text; string ChangNum = bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent().text; bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent().text = targetNum; partar.GetChild(0).GetComponent().text = ChangNum; //----------更换物品--- bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).GetComponent().partar = partar; bagUI.Instance.bagBakcGround.transform.GetChild(i).GetChild(1).parent = partar; partar.GetChild(1).localPosition = new Vector3(0, 0, 0); transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i); transform.localPosition = new Vector3(0, 0, 0); partar = transform.parent; return; } } else if(bagUI.Instance.bagBakcGround.transform.GetChild(i).childCount 25.0f) { string parNum = partar.GetChild(0).GetComponent().text; partar.GetChild(0).GetComponent().text = ""; transform.parent = bagUI.Instance.bagBakcGround.transform.GetChild(i); transform.localPosition = new Vector3(0, 0, 0); partar = transform.parent; partar.GetChild(0).GetComponent().text = parNum; return; } } }

交换物品位置是需要考虑交换到目标的格子中是否为空。

3、排列背包物品

当背包中的物品排列杂乱无章,有的格子有物品,有的格子没有,这时就需要排列背包中的物品。

/// /// 整理背包 /// void FinishingBag() { Dictionary ObjNum = new Dictionary(); for (int i = 0; i < bagBakcGround.transform.childCount; i++) { if(bagBakcGround.transform.GetChild(i).GetChild(0).GetComponent().text != "") { ObjNum.Add(bagBakcGround.transform.GetChild(i).GetChild(0), bagBakcGround.transform.GetChild(i).GetChild(1).gameObject); } } List ObjKey = new List(ObjNum.Keys); for (int i = 0; i < bagBakcGround.transform.childCount; i++) { if(bagBakcGround.transform.GetChild(i).childCount


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭