Unity进阶第二章-消息框架(实现角色吃金币功能)

一、消息框架概述

在这里插入图片描述

二、Framework代码实现

首先创建一个Framework文件夹
在这里插入图片描述

先写Message类:

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 int Audio_PlaySound = 100;
    public static int Audio_PlayMusic = 101;
    public static int Audio_StopMusic = 102;
    public static int Audio_ChangeVolume = 103;
    public static int UI_ShowPanel = 200;
    public static int UI_AddScore = 201;
    public static int UI_ShowShop = 202;

}


然后写MonoBase类:

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

public class MonoBase : MonoBehaviour
{
    
    
    //发送消息
    public void SendCustomMessage(Message msg)
    {
    
    
        MessageCenter.SendMessage(msg);
    }
    public void SendCustomMessage(byte type,int command,object content)
    {
    
    
        MessageCenter.SendMessage(type, command, content);
    }
    //接受消息
    public virtual void ReceiveMessage(Message message)
    {
    
    

    }
}


MessageCenter:

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

public class MessageCenter 
{
    
    
    // 管理类集合
    public static List<MonoBase> Managers = new List<MonoBase>();
    //发送消息
    public static void SendMessage(Message msg)
    {
    
    
        foreach(MonoBase mb in Managers)
        {
    
    
            mb.ReceiveMessage(msg);
        }
    }
    public static void SendMessage(byte type,int command,object content)
    {
    
    
        Message msg = new Message(type, command, content);
        SendMessage(msg);
    }
 
}

ManageBase:

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

public class ManageBase<T>: MonoBase where T:MonoBase
{
    
    
    public static T Instance;
    //管理的消息接收者
    public List<MonoBase> ReceiveList = new List<MonoBase>();
    //当前管理类接受的消息
    protected byte messageType;
    protected virtual void Awake()
    {
    
    
        Instance = this as T;
        //设置消息类型
        messageType = SetMessageType();
        //将当前的管理类添加到信息中心列表中
        MessageCenter.Managers.Add(this);
    }
    //必须实现,返回当前管理类的类型
    protected virtual byte SetMessageType()
    {
    
    
        return MessageType.Type_UI;
    }
    //注册消息监听
    public void RegisterReciver(MonoBase mb)
    {
    
    
        if (!ReceiveList.Contains(mb))
        {
    
    
            ReceiveList.Add(mb);
        }
    }
    //接收到了消息,并向下分发消息
    public override void ReceiveMessage(Message message)
    {
    
    
        base.ReceiveMessage(message);
        //如果接受到的消息不匹配。则不向下分发消息
        if (message.Type != messageType)
        {
    
    
            return;
        }
        foreach (MonoBase mb in ReceiveList)
        {
    
    
            mb.ReceiveMessage(message);
        }
    }


}

三、实例演示

(1)新建一个平面和一个胶囊,胶囊给它命名为Player
在这里插入图片描述
(2)给player加上刚体,并为它附加一个脚本PlayerControl
在这里插入图片描述
(3)编写PlayerControl脚本,首先实现让角色移动的功能

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

public class PlayerControl : MonoBehaviour
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
        //让角色移动
        float vertical = Input.GetAxis("Vertical");
        float horizontal = Input.GetAxis("Horizontal");
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        if (dir != Vector3.zero)
        {
    
    
            transform.Translate(dir * 3 * Time.deltaTime);
        }
    }
}

(4)新建一个圆柱体把它弄成金币的形状,打开它的触发器,并且给它加上CoinControl脚本,然后做成一个预设体

在这里插入图片描述
在这里插入图片描述

(5)创建一个分数面板,并为它添加一个脚本ScoreControl
在这里插入图片描述
(6)创建一个UIManage脚本

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

public class UIManager : ManageBase<UIManager>
{
    
    
    //设定接受的消息类型
    protected override byte SetMessageType()
    {
    
    
        return MessageType.Type_UI;
    }

}

(7)编写ScoreControl脚本,关联Text

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

public class ScoreContrl : MonoBase
{
    
    

    public Text text;
    private int score = 0;
     void Start()
        {
    
    
            //将当前类注册到UI管理类中,接受消息
            UIManager.Instance.RegisterReciver(this);
        }
    public override void ReceiveMessage(Message message)
    {
    
    
        base.ReceiveMessage(message);
        if (message.Command == MessageType.UI_AddScore)
        {
    
    
            //添加分数
            int add = (int)message.Content;
            score += add;
            text.text = score + "";
        }
    }
}


(8)编写CoinControl脚本,管理Coin

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

public class CoinControl : MonoBase
{
    
    
    // Start is called before the first frame update
    void Start()
    {
    
    
        
    }

    // Update is called once per frame
    void Update()
    {
    
    
       
    }
    private void OnTriggerEnter(Collider other)
    {
    
    
        if (other.tag == "Player")
        {
    
    
            //加分
            SendCustomMessage(MessageType.Type_UI,MessageType.UI_AddScore,1);
            //销毁自身
            Destroy(gameObject);
        }
    }
}

(9)记得把UIManager关联到Canvas上,并把勾取消

在这里插入图片描述
(10)运行游戏,一个吃金币效果就做好了

猜你喜欢

转载自blog.csdn.net/weixin_43853077/article/details/125905157