【UnityUI程序框架】The PureMVC Framework核心你會用嗎

在這里插入圖片描述


👨?💻個人主頁:@元宇宙-秩沅

👨?💻 hallo 歡迎 點贊👍 收藏? 留言📝 加關注?!

👨?💻 本文由 秩沅 原創

👨?💻 收錄于專欄:Unity基礎實戰

?🅰??



文章目錄

    • ?🅰??
    • ?前言?
    • 🎶(==A==) PureMVC的構成
    • 🎶(==0==) 通知名類PureNotification創建
    • 🎶(==1==) Model_Proxy(代理)
    • 🎶 Model_Proxy 實踐
    • 1.首先創建數據類型_PlayerDataObj
    • 2.創建代理_PlayerProxy
    • 🎶(==2==) View_Mediator(中介)
    • 🎶 View_Mediator 實踐
    • 1.首先創建主視圖(面板)_MainView
    • 2.創建中介_MainViewMediator
    • 3.創建角色視圖(面板)_RoleView
    • 4.創建中介_RoleViewMediator
    • ?🅰??


?前言?

PureMVC是一個基于模型-視圖-控制器(MVC)架構的開源框架,用于創建面向對象的、可重用的應用程序。它提供了一套成熟的模式和標準,以幫助開發人員實現松散耦合的代碼,以便更好地分離關注點和簡化應用程序的開發和維護。PureMVC框架適用于各種編程語言,包括Java、ActionScript、C#、PHP、JavaScript等,并且它已經被廣泛地應用于各種類型的應用程序中,包括桌面應用程序、Web應用程序和移動應用程序等。
官方網址:http://puremvc.org/

在這里插入圖片描述

  • dll導入法(更安全)
    在這里插入圖片描述
    在這里插入圖片描述
    在這里插入圖片描述
    在這里插入圖片描述
    將dll文件拖入新建女的Plug插件文件夾中
    在這里插入圖片描述
    在這里插入圖片描述

  • 移入Unity’中


🎶(A PureMVC的構成


在這里插入圖片描述

  • PureMVC由三個文件夾組成分別是 Core ,Interfaces,Patterns
    在這里插入圖片描述

在這里插入圖片描述

PureMVC中涉及到的設計模式

1.代理設計模式
2.中介者設計模式
3.外觀設計模式
4.觀察者設計模式
5.命令設計模式
6.單例設計模式


🎶(0 通知名類PureNotification創建


  • 觀察者設計模式
  • 原因:在PureMVC中每個模塊之間的聯系都是通過通知來進行連接在這里插入圖片描述
  • 所以需要創建一個通知名類
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 這個是pureMVC中的 通知名類
/// 主要是用來申明各個通知的 名字 
/// 方便使用和管理
/// </summary>
public class PureNotification 
{/// <summary>/// 啟動通知/// </summary>public const string START_UP = "startUp";/// <summary>/// 顯示面板通知/// </summary>public const string SHOW_PANEL = "showPanel";/// <summary>/// 隱藏面板通知/// </summary>public const string HIDE_PANEL = "hidePanel";/// <summary>/// 代表玩家數據更新的通知名/// </summary>public const string UPDATE_PLAYER_INFO = "updatePlayerInfo";/// <summary>/// 升級通知/// </summary>public const string LEV_UP = "levUp";
}

🎶(1 Model_Proxy(代理)


  • 代理設計模式

Proxy
由一個常量,兩個屬性,一個構造函數,兩個虛方法組成

在這里插入圖片描述

using PureMVC.Interfaces;
using PureMVC.Patterns.Observer;
namespace PureMVC.Patterns.Proxy
{public class Proxy: Notifier, IProxy{///代理名字       public const string NAME = "Proxy";///構造函數public Proxy(string proxyName, object data = null){ProxyName = proxyName ?? NAME;if (data != null) Data = data;}/// <summary>/// 當代理被注冊時的邏輯(由Model調用)Called by the Model when the Proxy is registered/// </summary>public virtual void OnRegister(){ }/// <summary>/// 當代理被移除時的邏輯(由Model調用)Called by the Model when the Proxy is registered/// </summary>public virtual void OnRemove(){}/// <summary>代理名(屬性)the proxy name</summary>public string ProxyName { get; protected set; }/// <summary>數據(屬性)the data object</summary>public object Data { get; set; }}
}

🎶 Model_Proxy 實踐


在這里插入圖片描述

1.首先創建數據類型_PlayerDataObj

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 玩家數據結構 
/// </summary>
public class PlayerDataObj
{//申明一堆玩家屬性相關的變量public string playerName;public int lev;public int money;public int gem;public int power;public int hp;public int atk;public int def;public int crit;public int miss;public int luck;
}

2.創建代理_PlayerProxy


using PureMVC.Patterns.Proxy;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 玩家數據代理對象
/// 主要處理 玩家數據更新相關的邏輯
/// </summary>
public class PlayerProxy : Proxy
{public new const string NAME = "PlayerProxy";//1.繼承Proxy父類//2.寫我們的構造函數//寫構造函數//重要點//1.代理的名字!!!!//2.代理相關的數據!!!!!public PlayerProxy():base(PlayerProxy.NAME){//在構造函數中 初始化一個數據 進行關聯PlayerDataObj data = new PlayerDataObj();//初始化data.playerName = PlayerPrefs.GetString("PlayerName", "唐老獅");data.lev = PlayerPrefs.GetInt("PlayerLev", 1);data.money = PlayerPrefs.GetInt("PlayerMoney", 9999);data.gem = PlayerPrefs.GetInt("PlayerGem", 8888);data.power = PlayerPrefs.GetInt("PlayerPower", 99);data.hp = PlayerPrefs.GetInt("PlayerHp", 100);data.atk = PlayerPrefs.GetInt("PlayerAtk", 20);data.def = PlayerPrefs.GetInt("PlayerDef", 10);data.crit = PlayerPrefs.GetInt("PlayerCrit", 20);data.miss = PlayerPrefs.GetInt("PlayerMiss", 10);data.luck = PlayerPrefs.GetInt("PlayerLuck", 40);//賦值給自己的Data進行關聯Data = data;}public void LevUp()//升級{PlayerDataObj data = Data as PlayerDataObj;//升級 改變內容data.lev += 1;data.hp += data.lev;data.atk += data.lev;data.def += data.lev;data.crit += data.lev;data.miss += data.lev;data.luck += data.lev;}public void SaveData()//存儲{PlayerDataObj data = Data as PlayerDataObj;//把這些數據內容 存儲到本地PlayerPrefs.SetString("PlayerName", data.playerName);PlayerPrefs.SetInt("PlayerLev", data.lev);PlayerPrefs.SetInt("PlayerMoney", data.money);PlayerPrefs.SetInt("PlayerGem", data.gem);PlayerPrefs.SetInt("PlayerPower", data.power);PlayerPrefs.SetInt("PlayerHp", data.hp);PlayerPrefs.SetInt("PlayerAtk", data.atk);PlayerPrefs.SetInt("PlayerDef", data.def);PlayerPrefs.SetInt("PlayerCrit", data.crit);PlayerPrefs.SetInt("PlayerMiss", data.miss);PlayerPrefs.SetInt("PlayerLuck", data.luck);}
}

🎶(2 View_Mediator(中介)


  • 中介者設計模式
    ——————————————在這里插入圖片描述
using PureMVC.Interfaces;
using PureMVC.Patterns.Observer;namespace PureMVC.Patterns.Mediator
{public class Mediator : Notifier, IMediator{public static string NAME = "Mediator";/// <summary>/// 構造函數./// </summary>/// <param name="mediatorName">中介名</param>/// <param name="viewComponent">面板名</param>public Mediator(string mediatorName, object viewComponent = null){MediatorName = mediatorName ?? NAME;ViewComponent = viewComponent;}/// <summary>/// 用來存儲通知名/// </summary>public virtual string[] ListNotificationInterests(){return new string[0];}/// <summary>/// 用來處理通知邏輯的方法/// </summary>/// <param name="notification">接口對象里面包含兩個重要的參數1.通知名2.通知包含的信息</param>public virtual void HandleNotification(INotification notification){}/// <summary>/// 注冊時執行的方法/// </summary>public virtual void OnRegister(){}/// <summary>/// 移除時執行的方法/// </summary>public virtual void OnRemove(){}/// <summary>中介名</summary>public string MediatorName { get; protected set; }/// <summary>(面板)名</summary>public object ViewComponent { get; set; }}
}

🎶 View_Mediator 實踐


1.首先創建主視圖(面板)_MainView

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class NewMainView : MonoBehaviour
{//1.找控件public Button btnRole;public Button btnSill;public Text txtName;public Text txtLev;public Text txtMoney;public Text txtGem;public Text txtPower;//2.提供面板更新的相關方法給外部//按照MVC的思想 可以直接在這里提供 更新的方法//如果是用MVP的思想public void UpdateInfo(PlayerDataObj data){txtName.text = data.playerName;txtLev.text = "LV." + data.lev;txtMoney.text = data.money.ToString();txtGem.text = data.gem.ToString();txtPower.text = data.power.ToString();}
}

2.創建中介_MainViewMediator

using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewMainViewMediator : Mediator
{public static new  string NAME = "NewMainViewMediator";//套路寫法//1.繼承PureMVC中的Mediator腳本 //2.寫構造函數public NewMainViewMediator():base(NAME){//這里面可以去創建界面預設體等等的邏輯//但是界面顯示應該是觸發的控制的//而且創建界面的代碼 重復性比較高}public void SetView(NewMainView view){ViewComponent = view;view.btnRole.onClick.AddListener(()=>{SendNotification(PureNotification.SHOW_PANEL, "RolePanel");});}//3.重寫監聽通知的方法public override string[] ListNotificationInterests(){//這是一個PureMVC的規則//就是你需要監聽哪些通知 那就在這里把通知們通過字符串數組的形式返回出去//PureMVC就會幫助我們監聽這些通知 // 類似于 通過事件名 注冊事件監聽return new string[]{PureNotification.UPDATE_PLAYER_INFO,};}//4.重寫處理通知的方法public override void HandleNotification(INotification notification){//INotification 對象 里面包含兩個隊我們來說 重要的參數//1.通知名 我們根據這個名字 來做對應的處理//2.通知包含的信息 switch (notification.Name){case PureNotification.UPDATE_PLAYER_INFO://收到 更新通知的時候 做處理if(ViewComponent != null){(ViewComponent as NewMainView).UpdateInfo(notification.Body as PlayerDataObj);}break;}}//5.可選:重寫注冊時的方法public override void OnRegister(){base.OnRegister();//初始化一些內容}
}

3.創建角色視圖(面板)_RoleView


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class NewRoleView : MonoBehaviour
{//1.找控件public Button btnClose;public Button btnLevUp;public Text txtLev;public Text txtHp;public Text txtAtk;public Text txtDef;public Text txtCrit;public Text txtMiss;public Text txtLuck;//2.提供面板更新的相關方法給外部public void UpdateInfo(PlayerDataObj data){txtLev.text = "LV." + data.lev;txtHp.text = data.hp.ToString();txtAtk.text = data.atk.ToString();txtDef.text = data.def.ToString();txtCrit.text = data.crit.ToString();txtMiss.text = data.miss.ToString();txtLuck.text = data.luck.ToString();}
}

4.創建中介_RoleViewMediator

using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class NewRoleViewMediator : Mediator
{public static new string NAME = "NewRoleViewMediator";//套路寫法//1.繼承PureMVC中的Mediator腳本 //2.寫構造函數public NewRoleViewMediator():base(NAME){}public void SetView(NewRoleView view){ViewComponent = view;//關閉按鈕 事件監聽view.btnClose.onClick.AddListener(() =>{SendNotification(PureNotification.HIDE_PANEL, this);});//升級按鈕監聽view.btnLevUp.onClick.AddListener(()=>{//去升級//去通知升級SendNotification(PureNotification.LEV_UP);});}//3.重寫監聽通知的方法public override string[] ListNotificationInterests(){return new string[] {PureNotification.UPDATE_PLAYER_INFO,//以后你還關心別的通知 就在這后面通過逗號連接 加起來就行了};}//4.重寫處理通知的方法public override void HandleNotification(INotification notification){//INotification 對象 里面包含兩個隊我們來說 重要的參數//1.通知名 我們根據這個名字 來做對應的處理//2.通知包含的信息 switch (notification.Name){case PureNotification.UPDATE_PLAYER_INFO://玩家數據更新 邏輯處理if(ViewComponent != null){(ViewComponent as NewRoleView).UpdateInfo(notification.Body as PlayerDataObj);}break;}}
}

?🅰??


?【Unityc#專題篇】之c#進階篇】

?【Unityc#專題篇】之c#核心篇】

?【Unityc#專題篇】之c#基礎篇】

?【Unity-c#專題篇】之c#入門篇】

?【Unityc#專題篇】—進階章題單實踐練習

?【Unityc#專題篇】—基礎章題單實踐練習

?【Unityc#專題篇】—核心章題單實踐練習


你們的點贊👍 收藏? 留言📝 關注?是我持續創作,輸出優質內容的最大動力!


在這里插入圖片描述


本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/17980.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/17980.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/17980.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python | Leetcode Python題解之第105題從前序與中序遍歷序列構造二叉樹

題目&#xff1a; 題解&#xff1a; class Solution:def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:if not preorder:return Noneroot TreeNode(preorder[0])stack [root]inorderIndex 0for i in range(1, len(preorder)):preorderVal pr…

rxjava BehaviorProcessor特性和使用說明

概念和說明 BehaviorProcessor 的定義 BehaviorProcessor 是 FlowableProcessor 的一個具體實現&#xff0c;它同時具備發布和訂閱的能力。它會保存最新的一個事件&#xff0c;并在新訂閱者訂閱時&#xff0c;立即將該事件發送給新訂閱者。 主要特性 緩存最新事件&#xff…

計算機畢業設計python+spark天氣預測 天氣可視化 天氣大數據 空氣質量檢測 空氣質量分析 氣象大數據 氣象分析 大數據畢業設計 大數據畢設

摘 要 近些年大數據人工智能等技術發展迅速&#xff0c;我國工業正努力從“制造”邁向“智造”實現新跨越。神經網絡(NeuronNetwork)是一種計算模型&#xff0c;通過大量數據的學習&#xff0c;來發現數據之間的模式和規律&#xff0c;模仿人腦神經元的工作方式。隨著算力的提…

音視頻集市應用融合平臺方案

音視頻應用即有深度又有廣度&#xff0c;如何讓一個平臺擁有更多功能更靈活的拓展能力&#xff0c;從單體模塊化&#xff0c;多插件到微服務都有大量的實踐。 筆者在實際開發過程也同樣面對這些紛繁復雜而又必須共容共通需求的挑戰。 在實戰開發了大量從服務端到設備端再到瀏覽…

vos3000外呼系統如何查詢授權信息和系統并發

要查詢VOS3000外呼系統的授權信息和系統并發情況&#xff0c;您可以按照以下步驟進行&#xff1a; 登錄系統管理界面&#xff1a; 使用管理員賬號登錄VOS3000外呼系統的管理界面。 查找系統信息&#xff1a; 尋找系統信息或授權管理的相關選項或標簽。 查詢授權信息&#xff…

五篇季度思想匯報

季度思想匯報一 尊敬的黨組織&#xff1a; 時光荏苒&#xff0c;轉眼間一個季度又過去了。在這一季度里&#xff0c;我經歷了許多&#xff0c;也有了不少的感悟和成長。 在工作中&#xff0c;我積極投入&#xff0c;努力提升自己的專業技能&#xff0c;面對各種任務和挑戰&am…

Linux:IPC - System V

Linux&#xff1a;IPC - System V 共享內存 shm創建共享內存shmgetshmctlftok 掛接共享內存shmatshmdt shm特性 消息隊列 msgmsggetmsgctlmsgsndmsgrcv 信號量 semSystem V 管理機制 System V IPC 是Linux系統中一種重要的進程間通信機制&#xff0c;它主要包括共享內存 shm&am…

物理內存與虛擬內存的區別

物理內存和虛擬內存是計算機系統中重要的概念&#xff0c;它們有著不同的特點和作用。 物理內存&#xff1a; 物理內存是計算機實際存在的內存&#xff0c;通常指的是RAM&#xff08;隨機存取存儲器&#xff09;。物理內存直接映射到計算機的物理地址空間&#xff0c;可以直接被…

? 傳知代碼 ? 高速公路車輛速度檢測軟件

&#x1f49b;前情提要&#x1f49b; 本文是傳知代碼平臺中的相關前沿知識與技術的分享~ 接下來我們即將進入一個全新的空間&#xff0c;對技術有一個全新的視角~ 本文所涉及所有資源均在傳知代碼平臺可獲取 以下的內容一定會讓你對AI 賦能時代有一個顛覆性的認識哦&#x…

【NumPy】全面解析NumPy的where函數:高效條件操作指南

&#x1f9d1; 博主簡介&#xff1a;阿里巴巴嵌入式技術專家&#xff0c;深耕嵌入式人工智能領域&#xff0c;具備多年的嵌入式硬件產品研發管理經驗。 &#x1f4d2; 博客介紹&#xff1a;分享嵌入式開發領域的相關知識、經驗、思考和感悟&#xff0c;歡迎關注。提供嵌入式方向…

哈希沖突的常見解決方法【附C++代碼】

在C中&#xff0c;哈希表是一種常用的數據結構&#xff0c;用于實現快速的插入、刪除和查找操作。 哈希表的核心在于哈希函數&#xff0c;它將輸入的關鍵字轉換為一個數組索引。然而&#xff0c;不同的關鍵字可能映射到相同的索引&#xff0c;這種情況稱為哈希沖突。 有效地解…

走進全球LED顯示龍頭艾比森,深挖逆勢增長43%的數智化邏輯

在大環境不景氣的情況下&#xff0c;有一家智能制造企業在2023年營收40億&#xff0c;同比增長高達43%&#xff0c;海外營收增長約 46%&#xff0c;并且連續12年單品牌出口額第一。 這就是全球LED顯示龍頭艾比森。 5月9日&#xff0c;紛享銷客帶領近70位企業高管走進紛享銷客…

使用Nginx將服務器目錄、文件共享出來

1.配置映射路徑&#xff0c;加入映射目錄 location /abc/ { autoindex on; autoindex_localtime on; charset utf-8; alias /usr/mydir/; } 2.重載Nginx配置 nginx -s reload 3.訪問 http://XXX.XXX.XXX.XXX/abc/ 即可 注&#xff1a; 如果…

短視頻再度重逢:四川京之華錦信息技術公司

短視頻再度重逢 在數字化時代的浪潮中&#xff0c;短視頻以其獨特的魅力迅速崛起&#xff0c;成為現代人生活中不可或缺的一部分。而當我們談論起短視頻&#xff0c;我們不僅僅是在談論一種娛樂方式&#xff0c;更是在談論一種情感的載體&#xff0c;一種回憶的媒介。今天&…

PHP8.0 match函數

match 表達式是 PHP 8.0 引入的一個新的控制結構&#xff0c;它提供了一種簡潔且更強大的方式來進行條件匹配。與 switch 語句相比&#xff0c;match 表達式具有以下優勢&#xff1a; 返回值&#xff1a;match 是一個表達式&#xff0c;它會返回一個值。嚴格比較&#xff1a;m…

MyBatis系統學習篇 - MyBatis逆向工程

MyBatis的逆向工程是指根據數據庫表結構自動生成對應的Java實體類、Mapper接口和XML映射文件的過程。逆向工程可以幫助開發人員快速生成與數據庫表對應的代碼&#xff0c;減少手動編寫重復代碼的工作量。 我們在MyBatis中通過逆向工具來幫我簡化繁瑣的搭建框架&#xff0c;減少…

iOS推送證書過期處理

蘋果推送證書的有效期都是一年&#xff0c;將要過期的時候&#xff0c;蘋果官方會發郵件提醒。 一、過期 在電腦上找到并打開其它->鑰匙串訪問&#xff1b; 我的證書可以看到各個App的推送證書&#xff0c;如果過期了&#xff0c;顯示紅色X 二、重新創建 1、登陸apple開…

如何解決三層單點故障

我給他整成下面這樣行不行呀 一個pc的默認網關只有一個&#xff0c;pc1配置的是1.1&#xff0c;那么路由壞了&#xff0c;他還是給1.1發送數據&#xff0c;冗余的那個也沒用上呀 用VRRP&#xff08;虛擬路由冗余協議&#xff09;解決以上問題 那光把這個R1和R2虛擬成一個R3&…

android usb轉串口

Android USB通信&#xff08;host轉串口&#xff09;_android usb 實現串口通信-CSDN博客

Windows內核函數 - 文件的讀操作

DDK提供了文件讀操作的內核函數&#xff0c;其函數聲明如下&#xff1a; NTSTATUS ZwWriteFile(IN HANDLE FileHandle,IN HANDLE Event,IN PIO_APC_ROUTINE ApcRoutine,IN PVOID ApcContext,out PIO_STATUS_BLOCK IoStatusBlock,IN PVOID Buffer,IN ULONG Length,IN PLARGE_IN…