『功能项目』事件中心【43】

我们打开上一篇42怪物的有限状态机的项目,

本章要做的事情是利用事件中心(和观察者模式相仿)将Update()函数中写的GameObject.Find()这些语句替换掉,因为在Update()函数中每帧的执行频率非常快,如果在Update()函数中写GameObject.Find()语句特别消耗性能,本章主题意义在于利用事件中心优化脚本代码,提升项目质量

首先创建脚本:IEventInfo.cs

cs 复制代码
using UnityEngine;
//事件的存储与管理 订阅者
public class IEventInfo : MonoBehaviour{
    
}

创建脚本:EventInfo.cs

cs 复制代码
using UnityEngine.Events;
//事件的存储与管理 订阅者
public class EventInfo : IEventInfo { 
    public UnityAction actions;
    public EventInfo(UnityAction actions){
        this.actions += actions;
    }
}
public class EventInfo<T> : IEventInfo {
    public UnityAction<T> actions;
    public EventInfo(UnityAction<T> action){
        actions = action;
    }
} 

创建脚本:EventCenter.cs

cs 复制代码
using System.Collections.Generic;
using UnityEngine.Events;
//中介者
public class EventCenter : Singleton<EventCenter>{
    //存储事件列表
    Dictionary<string,IEventInfo> eventDic = new Dictionary<string,IEventInfo>();
    //订阅消息
    public void AddEventListener(string name, UnityAction action) {
        if (eventDic.ContainsKey(name)){
            (eventDic[name] as EventInfo).actions += action;
        }
        else {
            eventDic.Add(name, new EventInfo(action));
        }
    }
    //通知消息
    public void EventTrigger(string name) {
        if (eventDic.ContainsKey(name)) {
            if ((eventDic[name] as EventInfo).actions != null) {
                (eventDic[name] as EventInfo).actions.Invoke();
            }
        }
    }
    public void RemoveEventListener(string name, UnityAction action) {
        if (eventDic.ContainsKey(name)) {
            (eventDic[name] as EventInfo).actions -= action;    
        }
    }
    //添加带参数事件的监听
    public void AddEventListener<T>(string name, UnityAction<T> action){
        //旧事件
        if (eventDic.ContainsKey(name)){
            (eventDic[name] as EventInfo<T>).actions += action;
        }
        //新事件
        else{
            eventDic.Add(name, new EventInfo<T>(action));
        }
    }
    //移除带参数事件的监听
    public void RemoveEventListener<T>(string name, UnityAction<T> action){
        if (eventDic.ContainsKey(name)){
            (eventDic[name] as EventInfo<T>).actions -= action;
        }
    }
    //分发带参数的事件
    public void EventTrigger<T>(string name,T info){
        if (eventDic.ContainsKey(name)){
            if ((eventDic[name] as EventInfo<T>).actions != null){
                (eventDic[name] as EventInfo<T>).actions.Invoke(info);
            }
        }
    }
    public void Clear() {
        eventDic.Clear();
    }
}

修改脚本:PlayerRayClickNavigation.cs

修改脚本:

运行项目 - 功能不变没有报错

本章利用事件中心(与观察者模式相仿)替换掉了Update()函数中的GameObject.Find()语句提升项目质量

接下来的内容:

1.战士职业平A(按A键)使怪物掉血的功能

2.窗口可拖拽脚本

3.点击名称寻找地点功能

4.隐藏怪物的生成

5.怪物I攻击范围内的主动攻击

6.掉落坐骑蛋的获取

7.异步传送转换场景

以及开放回合制、坐骑系统、宠物系统、背包系统、神炼系统、商城系统、Boss的目标跟随任务导航系统以及UI播放3D动画效果等等。

具体项目运行效果请关注water1024的b站视频项目演示《破碎纪元》

【Unity回合2.5D】破碎纪元_单机游戏热门视频 (bilibili.com)https://www.bilibili.com/video/BV1rZY4e9Ebs/?spm_id_from=333.999.0.0&vd_source=547091a95b03acfa8e8a9e46ef499cd6

相关推荐
唐青枫8 小时前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62512 小时前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021113 小时前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
小码编匠1 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net
唐青枫3 天前
别再乱用 StartNew:C#.NET TaskFactory 任务调度实战详解
c#·.net
Artech4 天前
[MAF预定义的AIContextProvider-03]ChatHistoryMemoryProvider——赋予Agent从经验中学习的能力
ai·c#·agent·memory·maf
Scout-leaf5 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6255 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#
Artech6 天前
[MAF预定义的AIContextProvider-02]AgentSkillsProvider——将Agent Skills引入MAF
ai·c#·agent·agent skills·maf
2601_962072556 天前
李梦娇常识4600问|题库|打印版
sql·华为od·华为·c#·华为云·.net·harmonyos