文章目录
- Unity进阶-消息框架的理论知识与实际操作学习笔记
Unity进阶-消息框架的理论知识与实际操作学习笔记
笔记来源课程:https://study.163.com/course/courseMain.htm?courseId=1212756805&_trace_c_p_k2_=8c8d7393c43b400d89ae94ab037586fc
这种框架其实就是分层管理了很多脚本,防止脚本调用产生的耦合性问题。
它的基础部分分为三个部分 脚本基础类,管理层基础类 ,以及命令类。
- 脚本基础类:一切功能脚本的模板
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoonBase : MonoBehaviour
{
public virtual void ReceiveMessage(Message message){
}
}
- 管理层类:所有管理层都来自于此
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();
}
- 命令基类:所有的命令都来自于此
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:泛型的单例类
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;
}
}
实际操作:使用消息框架制作吃金币功能
-
创建消息中心
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); } } }
随便找个对象绑定上
- 管理层–UI管理
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上
-
功能脚本–控制页面显示
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上
-
控制玩家触发
-
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));
}
}
}
绑定到主角胶囊上:
功能就完成了!!
复盘: