unity简单的背包制作 |
您所在的位置:网站首页 › 简单的皮包制作 › unity简单的背包制作 |
背包系统基本功能:添加物品、删除物品、拖拽物品、交换物品位置、排列物品等等。 首先在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 |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |