当前位置:   article > 正文

Unity开发之观察者模式(事件中心)_unity事件中心

unity事件中心

观察者模式是一种对象行为模式。它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。在观察者模式中,主体是通知的发布者,它发出通知时并不需要知道谁是它的观察者,可以有任意数目的观察者订阅并接收通知。

一、事件中心管理类

继承了一个单例模式基类,节省代码

  1. public class EventCenter : BaseManager<EventCenter>
  2. {
  3. //key —— 事件的名字(比如:怪物死亡,玩家死亡,通关 等等)
  4. //value —— 对应的是 监听这个事件 对应的委托函数们
  5. private Dictionary<string, UnityAction<object>> eventDic = new Dictionary<string, UnityAction<object>>();
  6. /// <summary>
  7. /// 添加事件监听
  8. /// </summary>
  9. /// <param name="name">事件的名字</param>
  10. /// <param name="action">准备用来处理事件 的委托函数</param>
  11. public void AddEventListener(string name, UnityAction<object> action)
  12. {
  13. //有没有对应的事件监听
  14. //有的情况
  15. if( eventDic.ContainsKey(name) )
  16. {
  17. eventDic[name]+= action;
  18. }
  19. //没有的情况
  20. else
  21. {
  22. eventDic.Add(name, action);
  23. }
  24. }
  25. /// <summary>
  26. /// 过场景,移除对应的事件监听
  27. /// </summary>
  28. /// <param name="name">事件的名字</param>
  29. /// <param name="action">对应之前添加的委托函数</param>
  30. public void RemoveEventListener(string name, UnityAction<object> action)
  31. {
  32. if (eventDic.ContainsKey(name))
  33. eventDic[name]-= action;
  34. }
  35. /// <summary>
  36. /// 事件触发
  37. /// </summary>
  38. /// <param name="name"></param>
  39. public void EventTrigger(string name,object info)
  40. {
  41. //有没有对应的事件监听
  42. //有的情况
  43. if (eventDic.ContainsKey(name))
  44. {
  45. if (eventDic[name]!= null)
  46. eventDic[name].Invoke(info);
  47. }
  48. }
  49. /// <summary>
  50. /// 清空事件中心
  51. /// 主要用在 场景切换时
  52. /// </summary>
  53. public void Clear()
  54. {
  55. eventDic.Clear();
  56. }
  57. }

二、模拟一个功能

玩家要完成一个任务“杀死三只哥布林”,玩家点击鼠标左键杀死一只怪物,如果这个怪物是哥布林,那么任务计数加1,达到3后提示任务完成。

其中还会显示玩家击杀了什么怪物,击杀后获得多少金钱

玩家类

  1. public class Player : MonoBehaviour
  2. {
  3. public string name = "Holens";
  4. // Start is called before the first frame update
  5. void Start()
  6. {
  7. EventCenter.GetInstance().AddEventListener("MonsterDead", GetMoney);
  8. }
  9. // Update is called once per frame
  10. void Update()
  11. {
  12. //按下左键杀死哥布林
  13. if (Input.GetMouseButtonDown(0))
  14. {
  15. KillMonster();
  16. }
  17. }
  18. public void KillMonster()
  19. {
  20. //杀死哥布林
  21. //向事件中心发送请求 执行杀死哥布林逻辑
  22. EventCenter.GetInstance().EventTrigger("KillMonster", this);
  23. }
  24. public void GetMoney(object info)
  25. {
  26. print(this.name+"杀死了" + (info as Monster).name + "获得"+ (info as Monster).money+"金钱");
  27. }
  28. }

怪物类

  1. public class Monster : MonoBehaviour
  2. {
  3. public string name;
  4. public int money = 100;
  5. // Start is called before the first frame update
  6. void Start()
  7. {
  8. //向 击杀哥布林 委托添加死亡函数
  9. EventCenter.GetInstance().AddEventListener("KillMonster", Dead);
  10. }
  11. public void Dead(object info)
  12. {
  13. print((info as Player).name+"杀死了"+this.name);
  14. //死亡时向事件中心发送请求 执行怪物死亡逻辑
  15. EventCenter.GetInstance().EventTrigger("MonsterDead", this);
  16. }
  17. }

任务类

  1. public class Task : MonoBehaviour
  2. {
  3. public int nowNum;
  4. public int maxNum = 3;
  5. public string messionInfo = "杀死三只哥布林";
  6. public bool isFinish = false;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. print("任务目标是" + messionInfo);
  11. //怪物死亡
  12. //向 怪物死亡 委托添加函数(当怪物死亡时任务计数加1)
  13. EventCenter.GetInstance().AddEventListener("MonsterDead", UpdateNum);
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if (nowNum == maxNum&&!isFinish)
  19. {
  20. //计数达到目标,任务完成
  21. FinishMession();
  22. isFinish = true;
  23. }
  24. }
  25. public void UpdateNum(object info)
  26. {
  27. //如果杀死的目标是哥布林,则计数增加
  28. if ((info as Monster).name=="哥布林")
  29. {
  30. nowNum++;
  31. }
  32. }
  33. public void FinishMession()
  34. {
  35. print("任务完成");
  36. }
  37. }

三、运行过程及结果

创建三个物体并挂载相应代码

怪物名字设置成1

此时按下鼠标左键五次,可以看到触发了提示但是因为杀的不是哥布林,所以任务没有完成

 将名字改为哥布林,点击三次后提示完成任务

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/865611
推荐阅读
相关标签
  

闽ICP备14008679号