文章目录
- Unity进阶-消息框架的理论知识与实际操作学习笔记
Unity进阶-消息框架的理论知识与实际操作学习笔记
笔记来源课程:https://study.163.com/course/courseMain.htm?courseId=1212756805&_trace_c_p_k2_=8c8d7393c43b400d89ae94ab037586fc
 
这种框架其实就是分层管理了很多脚本,防止脚本调用产生的耦合性问题。
它的基础部分分为三个部分 脚本基础类,管理层基础类 ,以及命令类。
- 脚本基础类:一切功能脚本的模板
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qZFBybQj-1689861770827)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720091438.png)]](https://img-blog.csdnimg.cn/36ab7ab9e22b480a94c33e5e2989d4ed.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoonBase : MonoBehaviour
{
    public virtual void ReceiveMessage(Message message){
        
    }
}
- 管理层类:所有管理层都来自于此
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wku9sHdm-1689861719422)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093919.png)]](https://img-blog.csdnimg.cn/df8c6d763a2440bdb112f736a07787f8.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xVjDmZaE-1689861719424)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093933.png)]](https://img-blog.csdnimg.cn/54d908fbd20e4e6491ed048b089025d4.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class ManagerBase : MyrSingletonBase<ManagerBase>
{
   public List<MoonBase> Monos = new List<MoonBase>();
   public void Register(MoonBase mono){
    if (!Monos.Contains(mono)){
        Monos.Add(mono);
    }
   }
   
   public virtual void ReceiveMessage(Message message){
    if (message.Type != GetMessageType()){
        return;
    }
    foreach (var mono in Monos){
        mono.ReceiveMessage(message);
    }
   }
   
   public abstract byte GetMessageType();
}
- 命令基类:所有的命令都来自于此
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xdRLGVHX-1689861719424)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720090818.png)]](https://img-blog.csdnimg.cn/0e18200dfc204b7bbea6f6087ce3e3bf.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AmlSHRaP-1689861719425)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720091245.png)]](https://img-blog.csdnimg.cn/77b6c4070bfd4c99b3361762a37123ff.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Message 
{
   public byte Type;
   public int Command ;
   public object Content;
   public Message() {}
   
   public Message(byte type, int command, object content) {
       Type = type;
       Command = command;
       Content = content;
   }
}
//消息类型
public class MessageType
{
    //类型
    public static byte Type_Audio = 1;
    public static byte Type_UI = 2;
    public static byte Type_Player = 3;
//声音命令
    public static int Audio_PlaySound = 100;
    public static int Audio_StopSound = 101;
    public static int Audio_PlayMusic = 102;
//UI命令
    public static int UI_ShowPanel = 200;
    public static int UI_AddScore = 201;
    public static int UI_ShowShop = 202;
}
DLC:泛型的单例类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tSG3uab4-1689861719426)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093200.png)]](https://img-blog.csdnimg.cn/b5d940624131422797f3565c5658f66d.png)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyrSingletonBase<T> : MonoBehaviour where T : MonoBehaviour
{
    private static T instance;
    public static T Instance {
        get
        {
            return instance;
        }
    }
    protected virtual void Awake() {
        instance = this as T;
    }
    protected virtual void OnDestroy() {
        instance = null;
    }
}
实际操作:使用消息框架制作吃金币功能
-  创建消息中心 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NWETV35X-1689861719427)(QQ%E6%88%AA%E5%9B%BE20230720094300.png)]](https://img-blog.csdnimg.cn/f7ec0f21c191448d80e2d40bc85fd381.png) using System.Collections; using System.Collections.Generic; using UnityEngine; public class MessageCenter : MyrSingletonBase<MessageCenter> { public List<ManagerBase> Managers = new List<ManagerBase>(); public void Register(ManagerBase manager){ if (!Managers.Contains(manager)){ Managers.Add(manager); } } public void SendCustomMessage(Message message){ foreach(var manager in Managers){ manager.ReceiveMessage(message); } } }随便找个对象绑定上 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xzSxfgW7-1689861719427)(../AppData/Roaming/Typora/typora-user-images/image-20230720212824307.png)]](https://img-blog.csdnimg.cn/ed27f3b13ffc4ce7a88bd4e6fd7e1823.png) - 管理层–UI管理
 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f8bFbyIH-1689861719428)(QQ%E6%88%AA%E5%9B%BE20230720102806.png)]](https://img-blog.csdnimg.cn/365b29179da54ac69c5918135e4d7772.png) using System.Collections; using System.Collections.Generic; using UnityEngine; public class UiManager : ManagerBase { void Start() { MessageCenter.Instance.Register(this); } public override byte GetMessageType() { return MessageType.Type_UI; } }绑定到canvas上 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YDTOy3qr-1689861719428)(../AppData/Roaming/Typora/typora-user-images/image-20230720212922833.png)]](https://img-blog.csdnimg.cn/6536ad26b1e94aa58597eebe1fb7a0b2.png) 
-  功能脚本–控制页面显示 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uLWKtfg1-1689861719429)(QQ%E6%88%AA%E5%9B%BE20230720103250.png)]](https://img-blog.csdnimg.cn/e92e571976c640be8bec937d104a5700.png) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Panel : MoonBase { public Text text; void Start() { UiManager.Instance.Register(this); } public override void ReceiveMessage(Message message){ base.ReceiveMessage(message); if(message.Command == MessageType.UI_AddScore){ int score = (int)message.Content; text.text = "分数" + score; } } } 绑定到panel上![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P1c4mK7h-1689861719429)(../AppData/Roaming/Typora/typora-user-images/image-20230720212946609.png)]](https://img-blog.csdnimg.cn/204f94c3d15e4a89a59ce883eeef7b6e.png) ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Rf3oaifv-1689861719430)(../AppData/Roaming/Typora/typora-user-images/image-20230720213232311.png)]](https://img-blog.csdnimg.cn/9c3b05ec32094e2199333a066bc44ebb.png) -  控制玩家触发 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vNw38Wk0-1689861719430)(QQ%E6%88%AA%E5%9B%BE20230720205232.png)]](https://img-blog.csdnimg.cn/197f23c26ee5429a8effbcac549fbef1.png) 
 ![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-apDxyOXK-1689861719430)(QQ%E6%88%AA%E5%9B%BE20230720103839.png)]](https://img-blog.csdnimg.cn/e37c58e616e9450e9665cb7007b6c7c8.png) 
-  
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerManager : MonoBehaviour
{
    int score = 0;
    void Start()
    {
        
    }
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        if (dir != Vector3.zero){
            transform.Translate(dir * 5 * Time.deltaTime);
        }
    }
    private void OnTriggerEnter(Collider other) {
        if(other.tag == "Coin"){
            score += 1;
            Destroy(other.gameObject);
            MessageCenter.Instance.SendCustomMessage(new Message(MessageType.Type_UI, MessageType.UI_AddScore, score));
        }
    }
}
绑定到主角胶囊上:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Lj1pWb9l-1689861719431)(../AppData/Roaming/Typora/typora-user-images/image-20230720212617176.png)]](https://img-blog.csdnimg.cn/870878ce42734b3d960bffd9d681d9c9.png)
功能就完成了!!
复盘:![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kH03RACn-1689861719431)(../Downloads/%E6%9C%AA%E5%91%BD%E5%90%8D%E6%96%87%E4%BB%B6.jpg)]](https://img-blog.csdnimg.cn/9d50f824026b464bb8ad8360d5429776.png)



















