Unity手游UGUI开发之背包界面(MVC)

来源:互联网 发布:小甲鱼java 编辑:程序博客网 时间:2024/05/21 19:48

一开始做背包系统的时候我是不知道MVC的,胡做一通,使用暴力实现后老板才教我原来背包系统用MVC框架实现才是正确的。

MVC简单来说就是Model View Controller。Model顾名思义,而View脚本则是把需要展现的Model展现到背包系统中,而Controller脚本,我则是用来实现道具的拖拽功能,道具使用,数量的增减。



背包系统中的拖拽功能实现我就不再多说了,前面有篇文章介绍到,这里的背包系统和拖拽方法  按道理上 是使用于任何类型的游戏。

然后还有个快捷栏,快捷栏的原理和背包系统差不多,只是在拖拽的时候,你要改下一判定能否落下,把快捷栏当成背包的格子,问题不大。


然后在这里我先谈一下整个制作的思路。

首先制作一个背包的Panel,里面和上篇制作商店界面时提到的一样,在背包Panel里放一个scrollview,里面初始化一堆格子。然后把数量大于0的道具按他的下标出现在对应的格子里,就是把该道具设置为某个格子的子物体。因为UGUI的特性,道具会遮盖住格子的效果。然后所有道具我是通过resources。load后instantiate在一个物体(我是GameManager的空物体)下。当他的数量大于0,他就会变成某个格子的子物体,自然就会出现在UI中啦。

这些道具是可以点击使用的:


点击使用后,会使该物体上的数量减1,当数量为0时,会移到GameManager空物体下(setparent)。同时,这些道具本身是有button组件的。点击后弹出details界面就不详细说了,挺简单的。

嗯,这里还有个和商店界面交互的问题,当购买会出现背包中最靠前的空闲格子上,因为shopmanager会把信息通知给bagmanager(通过修改bagmanager中的prop【i】中的count属性,并标志一下说有道具变动,通知bagmanager刷新一下)bagmanager也会检测进行刷新,同时在拖拽后或者使用物品后,bagmanager都在遍历所有格子,得出当前最靠前的空闲格子。所以这功能就不成问题。下面放源码


prop类(背包中的物品)是继承item类(物品)的,为什么这样,因为Item是Model读出来的,商店以及其他地方也可以重用该类,这里体现重用,代码精简的思想。

而我们所有设计的道具都带有prop类,并把这些prop存放在一个prop【】数组中,这个数组在GameManager(或者其他,也没关系)的脚本上。

bagmanager.cs

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class BagManager : MonoBehaviour {public GameObject[] Props=new GameObject[30];public int[] allindex=new int[34];public int firstfreeindex; public int curprop;public  GameManager gm;publicObject[] Propsobj;public GameObject bagview;public Text text;public Props prop;public Item item;public int curindex=0;public int changed=-1;// Use this for initializationvoid Start () {gm = GameObject.Find ("GameManager").GetComponent<GameManager>() ;Propsobj  = Resources.LoadAll("Icon");for (int i = 0; i < Propsobj.Length; i++) {Props [i] =Instantiate( Propsobj [i] )as GameObject;prop = Props [i].GetComponent<Props> ();item = Props [i].GetComponent<Item> ();prop.enabled = true;prop.ID = item.ID;prop.name = item.name;prop.details = item.details;prop.image = item.image;prop.value = item.value;prop.valueStyle = item.valueStyle;prop.quality = item.quality;Props [i].name = "prop_" + i;item.enabled = false;        Props [i].GetComponent<Drag2> ().enabled = true;Props [i].GetComponent<Button> ().enabled = true;if (prop.count > 0) {allindex[curindex] = 1;UnityEngine.UI.Texta=Instantiate (text, Props [i].transform) as UnityEngine.UI.Text;a.text = prop.count.ToString();a.rectTransform.localPosition = new Vector3 (25,-20);Props [i].transform.parent = bagview.transform.GetChild (curindex);Props [i].transform.localPosition = Vector3.zero;prop.index = curindex;    curindex++;}}}// Update is called once per framevoid Update () {if (changed == 1) {for (int k = allindex.Length-1; k>=0; k--) {if (allindex [k] == 0) {firstfreeindex = k;}}for (int i = 0; i < Propsobj.Length; i++) {prop = Props [i].GetComponent<Props> ();//道具数量为0if(prop.count<=0){//allindex [prop.index] = 0;prop.index = -1;Props [i].transform.parent =gm.transform ;changed = -1;}//道具的数量改变,且已有坐标if (prop.count > 0&&prop.index>=0) {Props [i].transform.FindChild ("CountText(Clone)").GetComponent<Text> ().text=prop.count.ToString();                    changed = -1;}//道具数量变为1,在背包中应生成  Index=-1即物体之前存放在GM中 if (prop.count > 0 && prop.index == -1 ){prop.index = firstfreeindex;Props [i].transform.FindChild ("CountText(Clone)").GetComponent<Text> ().text=prop.count.ToString();Props [i].transform.parent = bagview.transform.GetChild (prop.index);Props [i].transform.localPosition = Vector3.zero;allindex [prop.index] = 1;changed = -1;}//道具放在快捷栏中  Index=-2即物体之前存放在快捷栏中if (prop.count > 0 && prop.index == -2 ){Props [i].transform.FindChild ("CountText(Clone)").GetComponent<Text> ().text=prop.count.ToString();changed = -1;}}}}}



shopmanager中相关部分代码
public GameObject soldbg;public GameObject curBuy;public int CurClickIconID;public void setsold(){shop_goods a = curBuy.GetComponent<shop_goods> ();a.sold = 1;bm.Props [a.goods_ID-1].GetComponent<Props> ().count++;bm.changed = 1;}

Item类
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Item : MonoBehaviour {public int ID;public int valueStyle;    //Gold=1,Diamond=2public int value;public int quality;       //无色=0,白色=1,蓝色=2,紫色=3,橙色=4public Sprite image;public string name;public string details;// Use this for initializationvoid Start () {}}
prop类

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;public class Props : Item {public int count=1;public int index=-1;// Use this for initializationvoid Start () {}}




0 0