Article directory
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
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
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
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
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
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
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
- Management – UI Management
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
-
Functional scripts – control page display
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上
-
control player trigger
-
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:
The function is complete! !
Review: