当前位置: 首页 > news >正文

运城市做网站价格运营推广公司

运城市做网站价格,运营推广公司,有侧边栏的网站,seo教程技术作用:访问其它脚本时,不直接访问,而是通过发送一条“命令”,让监听了这条“命令”的脚本自动执行对应的逻辑。 原理: 1、让脚本向事件中心添加事件,监听对应的“命令”。 2、发送“命令”,事件…

作用:访问其它脚本时,不直接访问,而是通过发送一条“命令”,让监听了这条“命令”的脚本自动执行对应的逻辑。

原理:
1、让脚本向事件中心添加事件,监听对应的“命令”。
2、发送“命令”,事件中心就会通知监听了这条“命令”的脚本,让它们自动执行对应的逻辑。

 

事件中心管理器:添加事件、发送命令

员工类   将方法注册进事件中心管理器

public class Cube : MonoBehaviour
{private void Awake(){EventCenterManager.Instance.AddListener("开工", Write);}public void Write(){transform.position += Vector3.right;Debug.Log("我是策划,我在写策划案");}
}

事件管理中心类

public class EventCenterManager : SingletonPatternBase<EventCenterManager>
{//键表示命令的名字//值表示命令具体要执行的逻辑Dictionary<string, UnityAction> eventsDictionary = new Dictionary<string, UnityAction>();public void AddListener(string key,UnityAction call){if (eventsDictionary.ContainsKey(key)){eventsDictionary[key] += call;}elseeventsDictionary.Add(key, call);}/// <summary>/// 取消监听的命令/// </summary>public void RemoveListener(string key,UnityAction call){if (eventsDictionary.ContainsKey(key)){eventsDictionary[key] -= call;}}//发送命令public void DisPatch(string key){if (eventsDictionary.ContainsKey(key)){eventsDictionary[key]?.Invoke();}}

想要调用方法就直接DIsPatch  命令名  调用

Stopwatch类测试性能

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.Events;/// <summary>
/// Stopwatch类的工具类,用于计算运行一段代码所用的时间
/// </summary>
public class StopwatchUtility
{/// <summary>/// 获取执行一段代码所需要的时间/// </summary>/// <param name="call"></param>/// <returns></returns>public static TimeSpan GetTime(UnityAction call){//声明一个计数器Stopwatch timer = Stopwatch.StartNew();//开启计时器timer.Start();//要测试什么代码就将代码放在这里call?.Invoke();//停止计时器timer.Stop();//返回时间信息return timer.Elapsed;}public void PrintTime(UnityAction call){UnityEngine.Debug.Log(GetTime(call));}
}

里氏替换原则的用法

自己写两个类来包裹两个不同的UnityAction,然后让他们继承自同一接口,就可以实现里氏替换原则

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;public class EventCenterManager : SingletonPatternBase<EventCenterManager>
{//键表示命令的名字//值表示命令具体要执行的逻辑Dictionary<string, IEventInfo> eventsDictionary = new Dictionary<string, IEventInfo>();public void AddListener(object command,UnityAction call){string key = command.GetType().Name + "_" + command.ToString();if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo).action += call;}elseeventsDictionary.Add(key,new EventInfo(call));}//传递带参数的委托public void AddListener<T>(object command,UnityAction<T> call){string key = command.GetType().Name + "_" + command.ToString() + "_" + typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo<T>).action += call;}elseeventsDictionary.Add(key, new EventInfo<T>(call));}/// <summary>/// 取消监听的命令/// </summary>public void RemoveListener(object command,UnityAction call){string key = command.GetType().Name + "_" + command.ToString();if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo).action -= call;}}//取消带有参数的监听事件public void RemoveListener<T>(object command,UnityAction<T> call){string key = command.GetType().Name + "_" + command.ToString() + "_" + typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo<T>).action -= call;}}//移除一条命令所对应的全部委托public void RemoveListeners(object command){string key = command.GetType().Name + "_" + command.ToString();if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo).action = null;}}//带有参数的移除一条命令中所有委托public void RemoveListeners<T>(object command){string key = command.GetType().Name + "_" + command.ToString() + "_" + typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo<T>).action = null;}}//发送命令public void DisPatch(object command){string key = command.GetType().Name + "_" + command.ToString();if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo).action?.Invoke();}}//给带参数的事件写的重载public void DisPatch<T>(object command,T parameter){string key = command.GetType().Name + "_" + command.ToString() + "_" + typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfo<T>).action?.Invoke(parameter);}}/// <summary>/// 移除所有带或者不带参数的监听事件,切换场景可以考虑使用/// </summary>public void RemoveAllListeners(){eventsDictionary.Clear();}private interface IEventInfo { }  //仅用于里氏替换原则private class EventInfo:IEventInfo{public UnityAction action;public EventInfo(UnityAction call){action += call;}}private class EventInfo<T> :IEventInfo{public UnityAction<T> action;public EventInfo(UnityAction<T> call){action += call;}}}

传递多个参数的委托事件:

  • 可以写一个信息类来存储多个信息,然后事件中传递这个信息类。这样就还是一个参数
  • 或者额外写含有两个参数的方法,写含有三个的,四个的(可行,但是费劲)
  • 元组(但是我不知道这个是什么)

http://www.hkea.cn/news/656284/

相关文章:

  • 餐饮公司网站建设的特点大数据智能营销
  • 济南快速排名刷关键词排名seo软件
  • 系统做网站的地方百度推广登录后台登录入口
  • 集约化网站建设情况广告公司网站制作
  • 网站制作发票字节跳动广告代理商加盟
  • 义乌做网站武汉seo推广优化公司
  • 济宁哪家网站建设公司正规谷歌浏览器 免费下载
  • 有没有女的做任务的网站广东省新闻
  • seo长尾关键词优化如何做网站推广优化
  • 网站搭建服务合同seo排名赚
  • 东莞有什么比较好的网站公司苏州关键词排名系统
  • 做中国供应商免费网站有作用吗浙江网站推广运营
  • mysql8 wordpress百度推广优化是什么意思
  • 做装修广告网站好seo推广公司招商
  • 城市模拟建设游戏网站今天最新的新闻头条新闻
  • 手机网站自适应代码品牌网络营销策划方案
  • 个人网站建设在哪里百度资源搜索平台
  • 云空间免费空间北京网站优化校学费
  • 个人网站做导航网站项目推广平台有哪些
  • 威海住房建设局网站培训学校资质办理条件
  • 做趣味图形的网站免费线上培训平台
  • 女生做网站前端设计师成都网站seo
  • 濮阳建设银行官方网站搜索引擎优化的对比
  • 完全删除wordpressseo小白入门
  • 做网站常用到的css标签什么软件可以找客户资源
  • 有做销售产品的网站有哪些新闻头条今日新闻
  • 深圳自己做网站 服务器优化的近义词
  • 网站开发职业工资网站推广上首页
  • 宝安附近公司做网站建设多少钱深圳百度开户
  • 成都紧急通知seo网络营销招聘