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;}}}}}
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 () {}}
- Unity手游UGUI开发之背包界面(MVC)
- Unity开发之 UGUI手游界面返回上一层
- unity手游UI界面开发之商店等界面
- Unity手游框架 之 界面管理(一)
- unity ugui手游自适应方法
- 【小松教你手游开发】【unity实用技能】NGUI研究院之为什么打开界面太慢
- Unity——UGUI之界面布局
- 【Unity】基于MVC模式的背包系统 UGUI实现
- unity手游<少侠历险记>(4)背包系统
- unity手游<少侠历险记>(4)背包系统
- unity开发之五:UGUI界面规范以及素材命名规范
- 游戏开发之UGUI搭建UI界面
- Unity使用UGUI进行VR游戏的界面开发
- Unity ugui拼界面规则
- LOR手游开发过程记录——菜单界面——NFC的Unity接口
- 十三 手游开发神器 cocos2d-x editor 之选关滑动界面(ScrollView)
- unity之跑马灯(UGUI+DOTween)
- unity之ugui
- 【Python】用C语言编写Python模块
- java中不太常见的东西(3)
- BootStrap(三) -- 通过tableExport实现数据导出
- ADG732: 32通道、串行控制、3.5 Ω、1.8 V至5.5 V/±2.5 V、模拟多路复用器
- TCP拥塞控制
- Unity手游UGUI开发之背包界面(MVC)
- 密电码
- 格式化秒,秒转换为: 时 分 秒的格式
- 电商行业
- endianness
- poj 2417 baby_step giant_step 高次同余方程
- 我想知道Java虚拟机是啥
- 代码中设置drawableleft
- MySQL 5.7.17 免安装版本的安装,配置及MySQL的密码修改