【UnityRPG游戏制作】PureMVC框架嵌入面板示例

发布于:2024-04-22 ⋅ 阅读:(179) ⋅ 点赞:(0)

在这里插入图片描述


👨‍💻个人主页@元宇宙-秩沅

👨‍💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!

👨‍💻 本文由 秩沅 原创

👨‍💻 收录于专栏Unity基础实战

🅰️




前言

请添加图片描述



🎶(1 状态面板示例


在这里插入图片描述

在这里插入图片描述


Main

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//-------------------------------
//-------功能:  主路口
//-------创建者:         -------
//------------------------------

public class Main : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        GameFacade.Instance.StartUp();
      
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}


PureNotification

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//-------------------------------
//-------功能: 通用的通知名类
//-------创建者:         -------
//------------------------------

public class PureNotification 
{
    public const string SHOW_PANEL = "ShowPanel";         //显示面板的通知
    public const string HIDE_PANEL = "HidePanel";         //显示面板的通知
    //public const string BACKPACK_PANEL = "BackpackPanel"; //背包面板通知
    //public const string DEFEAT_PANEL = "DefeatPanel";     //失败面板通知
    //public const string GAME_PANEL   = "GamePanel";       //游戏面板通知
    //public const string NPCTIP_PANEL = "NPCTipPanel";     //NPC交互面板通知
    //public const string ROLE_PANEL   = "RolePanel";       //角色面板通知
    //public const string START_PANEL  = "StartPanel";      //开始面板通知
    //public const string STARTTIP_PANEL = "StartTipPanel"; //开始说明面板通知
    //public const string STATE_PANEL    = "StatetPanel";   //状态面板通知
    public const string UPDATA_ROLE_INFO  = "updataRoleInfo"; //角色信息更新通知
    public const string UPDATA_STATE_INFO = "playerProxy";//角色状态更新通知
    public const string START_UP = "startUp"; //启动通知
}


GameFacade

using PureMVC.Patterns.Facade;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//-------------------------------
//-------功能:  游戏外观
//-------创建者:         -------
//------------------------------


/// <summary>
/// 游戏外观
///1.继承PureMvc中Facade脚本
///2.为了方便使用Facade,需要自己写一个单例模式的属性
///3.初始化控制层相关的内容
///4.构造一个启动函数
/// </summary>
public class GameFacade : Facade
{
    //设置成单例模式
    public static GameFacade Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new GameFacade();
            }
            return instance as GameFacade;
        }
    }

    /// <summary>
    /// 3.初始化控制层相关的内容
    /// </summary>
    protected override void InitializeController()
    {
        base.InitializeController();
        //注册开始命令
        RegisterCommand(PureNotification.START_UP,()=>
        {
            return new StartCommand();     //里氏替换原则
        }
        );

        //注册显示面板的命令
        RegisterCommand(PureNotification.SHOW_PANEL, () =>
        {
            return new ShowPanelCommand();     //里氏替换原则
        }
        );

        //注册隐藏面板的命令
        RegisterCommand(PureNotification.HIDE_PANEL, () =>
        {
            return new HidePanelCommand();     //里氏替换原则
        }
        );
    }

    /// <summary>
    /// 启动函数
    /// </summary>
    public void StartUp() 
    {
        //发送启动的通知
        SendNotification(PureNotification.START_UP);
        SendNotification(PureNotification.SHOW_PANEL , "StatePanel");
    }

}


ShowPanelCommand

using PureMVC.Interfaces;
using PureMVC.Patterns.Command;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

//-------------------------------
//-------功能: 面板显示命令
//-------创建者:      
//------------------------------

public class ShowPanelCommand : SimpleCommand 
{
    /// <summary>
    /// 执行逻辑
    /// </summary>
    /// <param name="notification"></param>
    public override void Execute(INotification notification)
    {
        base.Execute(notification);
        Debug.Log("显示面板的命令");
        string panelName = notification.Body.ToString();
        //Debug.Log(panelName);
        SelectPanel(panelName);
    }

    /// <summary>
    /// 封装选择需要执行的面板
    /// </summary>
    /// <param name="panelName"></param>
    private void SelectPanel(string panelName)
    {
        //面板命令的选择
        switch (panelName)
        {
            case "BackpackPanel":
                Debug.Log("命令为BackpackPanel");
                if (!Facade.HasMediator(BackpackViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new BackpackViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                BackpackViewMediator bm = Facade.RetrieveMediator(BackpackViewMediator.NAME) as BackpackViewMediator;

                if (bm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<BackpackView>("BackpackPanel", (panelobj) => {
                        bm.ViewComponent = panelobj;
                        Debug.Log(bm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "DefeatPanel":
                Debug.Log("命令为DefeatPanel");
                if (!Facade.HasMediator(DefeatViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new DefeatViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                DefeatViewMediator dm = Facade.RetrieveMediator(DefeatViewMediator.NAME) as DefeatViewMediator;

                if (dm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<DefeatView>("DefeatPanel", (panelobj) => {
                        dm.ViewComponent = panelobj;
                        Debug.Log(dm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "GamePanel":
                Debug.Log("GamePanel");
                if (!Facade.HasMediator(GameViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new GameViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                GameViewMediator gm = Facade.RetrieveMediator(GameViewMediator.NAME) as GameViewMediator;

                if (gm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<GameView>("GamePanel", (panelobj) => {
                        gm.ViewComponent = panelobj;
                        Debug.Log(gm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "NPCTipPanel":
                Debug.Log("NPCTipPanel");
                if (!Facade.HasMediator(NPCTipViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new NPCTipViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                NPCTipViewMediator nm = Facade.RetrieveMediator(NPCTipViewMediator.NAME) as NPCTipViewMediator;

                if (nm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<NPCTipView>("NPCTipPanel", (panelobj) => {
                        nm.ViewComponent = panelobj;
                        Debug.Log(nm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "RolePanel":
                Debug.Log("命令为RolePanel");
                if (!Facade.HasMediator(RoleViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new RoleViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                RoleViewMediator rm = Facade.RetrieveMediator(RoleViewMediator.NAME) as RoleViewMediator;

                if (rm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<RoleView>("RolePanel", (panelobj) => {
                        rm.ViewComponent = panelobj;
                        Debug.Log(rm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "StartPanel":
                Debug.Log("StartPanel");
                if (!Facade.HasMediator(StartViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new StartViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                StartViewMediator sm = Facade.RetrieveMediator(StartViewMediator.NAME) as StartViewMediator;

                if (sm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<StartView>("StartPanel", (panelobj) => {
                        sm.ViewComponent = panelobj;
                        Debug.Log(sm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "StartTipPanel":
                Debug.Log("StartTipPanel");
                if (!Facade.HasMediator(StartTipViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new StartTipViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                StartTipViewMediator stm = Facade.RetrieveMediator(StartTipViewMediator.NAME) as StartTipViewMediator;

                if (stm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                    UIManager.GetInstance().ShowPanel<StartTipView>("StartTipPanel", (panelobj) => {
                        stm.ViewComponent = panelobj;
                        Debug.Log(stm.ViewComponent.ToString()); //打印对应的面板名字
                    });
                }
                break;
            case "StatePanel":
                Debug.Log("命令为StatePanel");
                if (!Facade.HasMediator(StateViewMediator.NAME))  //首先判断是否有该中介,没有就new一个
                {
                    Facade.RegisterMediator(new StateViewMediator()); //注册该视图中介
                }
                //获取视图对应的中介
                StateViewMediator mm = Facade.RetrieveMediator(StateViewMediator.NAME) as StateViewMediator;

                if (mm.ViewComponent == null) //当对应的视图中介没有关联界面面板时 
                {
                    //用UI管理器获取对应面板,然后与中介的ViewComponent界面视图同步关联
                     UIManager.GetInstance().ShowPanel<StateView>("StatePanel", (panelobj) => {
                     mm.setView(panelobj);        
                     Debug.Log(mm.ViewComponent.ToString()+"成功赋值"); //打印对应的面板名字
                    });
                }
              
                //实现面板之后,数据需要进行更新
                SendNotification(PureNotification.UPDATA_STATE_INFO ,Facade.RetrieveProxy(PlayerProxy.NAME).Data  );
                break;       
        }
    }
}

StateViewMediator

using PureMVC.Core;
using PureMVC.Interfaces;
using PureMVC.Patterns.Mediator;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//-------------------------------
//-------功能:  状态面板视图中介
//-------创建者:         
//------------------------------


/// <summary>
/// 状态面板视图中介
/// 固定:
/// 1.继承PureMVC的Mediator脚本
/// 2.写构造函数
/// 3.重写监听通知的方法
/// 4.重写处理通知的方法
/// 5.可选:重写注册时的方法
/// </summary>
public class StateViewMediator : Mediator
{
    //铭牌名
    public static string NAME = "StateViewMediator";

    /// <summary>
    /// 构造函数
    /// </summary>
    public StateViewMediator() : base(NAME)
    {
    }

    /// <summary>
    /// 重写监听通知的方法
    /// </summary>
    /// <returns>返回你需要监听的通知的名字数组</returns>
    public override string[] ListNotificationInterests()
    {
        return new string[] {
         PureNotification.UPDATA_STATE_INFO,

        };
    }

    /// <summary>
    /// 面板中组件设置(监听相关)
    /// </summary>
    /// <param name="stateView"></param>
    public void setView(StateView stateView )
    {
        ViewComponent = stateView;
        stateView.roleBtu.onClick.AddListener(()=>
        {
            SendNotification(PureNotification.HIDE_PANEL, "StatePanel");
            SendNotification(PureNotification.SHOW_PANEL, "RolePanel");
        });
        stateView.backpackBtu.onClick.AddListener(() =>
        {
            SendNotification(PureNotification.HIDE_PANEL, "StatePanel");
            SendNotification(PureNotification.SHOW_PANEL, "BackpackPanel");
        });
    }
       

    /// <summary>
    /// 重写处理通知的方法,处理通知
    /// </summary>
    /// <param name="notification">通知</param>
    public override void HandleNotification(INotification notification)
    {
        switch (notification.Name)
        {
            case PureNotification.UPDATA_STATE_INFO:
             
                if (ViewComponent !=null)
                {
                    (ViewComponent as StateView).UpdateView(notification.Body as PlayerDataObj);

                }
                else
                {
                    Debug.Log("为空");

                }
                break;
        }
    }
}


🎶(1


🅰️


【Unityc#专题篇】之c#进阶篇】

【Unityc#专题篇】之c#核心篇】

【Unityc#专题篇】之c#基础篇】

【Unity-c#专题篇】之c#入门篇】

【Unityc#专题篇】—进阶章题单实践练习

【Unityc#专题篇】—基础章题单实践练习

【Unityc#专题篇】—核心章题单实践练习


你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!


在这里插入图片描述



网站公告

今日签到

点亮在社区的每一天
去签到