0
点赞
收藏
分享

微信扫一扫

吴恩达2022机器学习专项课程C2W1:(选修)2.13 如何高效实施神经网络 & 2.14 矩阵乘法 & 2.15 矩阵乘法规则 & 矩阵乘法代码

代码小姐 2024-05-25 阅读 9

在这里插入图片描述


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

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

👨‍💻 本文由 秩沅 原创

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

🅰️



文章目录


前言

在这里插入图片描述

  • dll导入法(更安全)
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    将dll文件拖入新建女的Plug插件文件夹中
    在这里插入图片描述
    在这里插入图片描述

  • 移入Unity’中


🎶(A PureMVC的构成


在这里插入图片描述

  • PureMVC由三个文件夹组成分别是 Core ,Interfaces,Patterns
    在这里插入图片描述

在这里插入图片描述


🎶(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(代理)


  • 代理设计模式

在这里插入图片描述

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#专题篇】—核心章题单实践练习


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


在这里插入图片描述


举报

相关推荐

0 条评论