首页天道酬勤,

,

admin 05-04 03:59 135次浏览

##### 感谢您的指出。

目录

前言

一、MVP模式是什么?

二.通过unity实现

1 .总体流程

2 .具体实现

总结

前言在游戏开发过程中,经常会遇到修改界面样式,但界面功能没有发生变化的情况。 如果可以将接口与逻辑分开,则更改将很少。 MVP模式可以分离界面的显示、操作、数据,适合经常修改的界面。

一、MVP模式是什么? MVP都被称为Model、View、Presenter。

模型,用于处理数据的模型;

View,显示模型中数据的视图;

用于处理Presenter、显示器、接口逻辑、View和Model的交互式更新等。

这些关系如下

View监听用户输入并将其发送到Presenter。 Presenter根据输入执行Model的相应数据更新操作,当Model的数据发生变化时通知Presenter,Presenter在收到通知时从Model获取数据更新视图。

与MVC不同,在MVP中m和v没有连接,所有逻辑操作都由Presenter进行,Presenter可以监听模型的通知并更新View。

二.在unity上1 .实现整体流程

2 .具体实现了显示角色基本信息的简单UI。

MVP接口

namespace MVP{ /* * Model接口*/publicinterfaceiuimodel { void init (} namespace MVP {/* * view接口*/publicinit () ) }

namespace MVP {/* *控制器接口*/publicinterfaceiuipresenter { void preinit (字符串路径) }; voidshow (对象数据; void Hide (; void Dispose (; }接口的实现

名称空间MVP { publicclassroleinfomodel : iui model { public string name; 公共字符串质量; 公共Int惠普; 公共插入攻击; 公共int defense; public void Init () { Name='jjddn '; 质量='极品'; 惠普Hp=100; Attack=200; Defense=300; }公共语音升级() {惠普=100; Attack =20; Defense =20; game events.trigger event (eeventtype.role info _ on upgrade; } }

using UnityEngine.UI; 用户单元引擎; namespace MVP { publicclassroleinfoview 3360 mono behavio }

ur, IUIView { [HideInInspector] public Text roleName; [HideInInspector] public Text quality; [HideInInspector] public Text hp; [HideInInspector] public Text attack; [HideInInspector] public Text defense; [HideInInspector] public Button upgrade; [HideInInspector] public Button close; [HideInInspector] public void PreInit() { roleName = transform.Find("bg/name").GetComponent<Text>(); quality = transform.Find("bg/qualityDesc/quality").GetComponent<Text>(); hp = transform.Find("bg/rightInfo/hp").GetComponent<Text>(); attack = transform.Find("bg/rightInfo/attack").GetComponent<Text>(); defense = transform.Find("bg/rightInfo/defense").GetComponent<Text>(); upgrade = transform.Find("bg/upgrade").GetComponent<Button>(); close = transform.Find("bg/close").GetComponent<Button>(); } }} using System;using System.Collections.Generic;using UnityEngine;namespace MVP{ public abstract class UIPresenterBase : MonoBehaviour, IUIPresenter { protected string uiPath; protected abstract IUIView View { get; } public virtual void PreInit(string path) { uiPath = path; } public abstract void Show(object data); public abstract void Hide(); public abstract void Dispose(); protected abstract void RegistEvents(); protected abstract void UnRegistEvents(); }}

 

using UnityEngine;namespace MVP{ public class RoleInfoPresenter : UIPresenterBase { protected override IUIView View { get { return _view; } } private RoleInfoView _view; private RoleInfoModel model; public override void PreInit(string path) { base.PreInit(path); _view = this.transform.GetComponent<RoleInfoView>(); _view.PreInit(); model = MVPModels.GetModel(path) as RoleInfoModel; if (model == null) { model = new RoleInfoModel(); model.Init(); MVPModels.RegistModel(path, model); } _view.upgrade.onClick.AddListener(OnClickUpgrade); _view.close.onClick.AddListener(OnClickClose); } public override void Show(object data) { RoleInfoModel model = MVPModels.GetModel(uiPath) as RoleInfoModel; _view.roleName.text = model.Name; _view.quality.text = model.Quality; _view.hp.text = model.Hp.ToString(); _view.attack.text = model.Attack.ToString(); _view.defense.text = model.Defense.ToString(); RegistEvents(); this.gameObject.SetActive(true); } public override void Hide() { this.gameObject.SetActive(false); UnRegistEvents(); } public override void Dispose() { } private void OnClickUpgrade() { upgradeRole(); } private void OnClickClose() { UIManager.Instance.HideUI(uiPath); } protected override void RegistEvents() { GameEvents.AddGameEvent(eEventType.RoleInfo_OnUpgrade, OnUpgraded); } protected override void UnRegistEvents() { GameEvents.RemoveGameEvent(eEventType.RoleInfo_OnUpgrade, OnUpgraded); } private void OnUpgraded() { RoleInfoModel model = MVPModels.GetModel(uiPath) as RoleInfoModel; _view.hp.text = model.Hp.ToString(); _view.attack.text = model.Attack.ToString(); _view.defense.text = model.Defense.ToString(); } public void upgradeRole() { model.Upgrade(); } }} using System;using System.Collections.Generic;namespace MVP{ public class MVPModels { private static Dictionary<string, IUIModel> uiModels = new Dictionary<string, IUIModel>(); public static void RegistModel(string type, IUIModel model) { if (!uiModels.ContainsKey(type)) { uiModels.Add(type, model); } } public static IUIModel GetModel(string type) { if (uiModels.ContainsKey(type)) { return uiModels[type]; } return null; } }}

MVPModels中存放所有界面的Model。 

代码的绑定:

 

GameEvents是事件管理器,可以注册和分发事件。

eEventType是事件Id的枚举。

 

总结

使用MVP模式制作游戏界面可以让显示和数据解耦,代码层次清晰,易于理解,MVP中View层代码因为没有什么逻辑,可以根据UI面板来生成代码,自动绑定。

 

 

UGUI实现ScrollView无限滚动效果硬件隔离组操作
你是我的mvp什么意思,mvp形容人什么意思 Android封装怎么用,androidview封装
相关内容