Unity advanced - theoretical knowledge and practical operation study notes of message framework

Unity advanced - theoretical knowledge and practical operation study notes of message framework

Note source course: https://study.163.com/course/courseMain.htm?courseId=1212756805&_trace_c_p_k2_=8c8d7393c43b400d89ae94ab037586fc
insert image description here

This framework actually manages many scripts hierarchically to prevent coupling problems caused by script calls.

Its basic part is divided into three parts: script base class, management base class, and command class.

  • Script base class: template for all functional scripts

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-qZFBybQj-1689861770827)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720091438.png)]

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

public class MoonBase : MonoBehaviour
{
    
    
    public virtual void ReceiveMessage(Message message){
    
    
        
    }
}

  • Management class: All management layers come from here

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-wku9sHdm-1689861719422)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093919.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-xVjDmZaE-1689861719424)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093933.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();
}

  • Command base class: all commands come from here

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-xdRLGVHX-1689861719424)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720090818.png)]

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-AmlSHRaP-1689861719425)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720091245.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: Generic Singleton Classes

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-tSG3uab4-1689861719426)(C:/Users/86188/Desktop/QQ%E6%88%AA%E5%9B%BE20230720093200.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;
    }
}

Practical operation: use the message framework to create the function of eating gold coins

  • Create message center

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-NWETV35X-1689861719427)(QQ%E6%88%AA%E5%9B%BE20230720094300.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);
            }
        }
    }
    
    

    Just find an object to bind to

    [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-xzSxfgW7-1689861719427)(../AppData/Roaming/Typora/typora-user-images/image-20230720212824307.png)]

    • Management – ​​UI Management

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-f8bFbyIH-1689861719428)(QQ%E6%88%AA%E5%9B%BE20230720102806.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;
        }
        
    }
    
    

    Bind to the canvas

    [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-YDTOy3qr-1689861719428)(../AppData/Roaming/Typora/typora-user-images/image-20230720212922833.png)]

  • Functional scripts – control page display

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-uLWKtfg1-1689861719429)(QQ%E6%88%AA%E5%9B%BE20230720103250.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上
    

    [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-P1c4mK7h-1689861719429)(../AppData/Roaming/Typora/typora-user-images/image-20230720212946609.png)]

    [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Rf3oaifv-1689861719430)(../AppData/Roaming/Typora/typora-user-images/image-20230720213232311.png)]

    • control player trigger

      [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-vNw38Wk0-1689861719430)(QQ%E6%88%AA%E5%9B%BE20230720205232.png)]

    [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-apDxyOXK-1689861719430)(QQ%E6%88%AA%E5%9B%BE20230720103839.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));
        }
    }
}

Bind to the protagonist capsule:

[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-Lj1pWb9l-1689861719431)(../AppData/Roaming/Typora/typora-user-images/image-20230720212617176.png)]

The function is complete! !

Review:[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-kH03RACn-1689861719431)(../Downloads/%E6%9C%AA%E5%91%BD%E5%90%8D%E6%96%87%E4%BB%B6.jpg)]

Guess you like

Origin blog.csdn.net/abaidaye/article/details/131840993