wordpress网站amp,要运行 wordpress php无法安装,广东平台网站建设哪家好,wordpress添加友情练级作用#xff1a;访问其它脚本时#xff0c;不直接访问#xff0c;而是通过发送一条“命令”#xff0c;让监听了这条“命令”的脚本自动执行对应的逻辑。
原理#xff1a; 1、让脚本向事件中心添加事件#xff0c;监听对应的“命令”。 2、发送“命令”#xff0c;事件…作用访问其它脚本时不直接访问而是通过发送一条“命令”让监听了这条“命令”的脚本自动执行对应的逻辑。
原理 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 : SingletonPatternBaseEventCenterManager
{//键表示命令的名字//值表示命令具体要执行的逻辑Dictionarystring, UnityAction eventsDictionary new Dictionarystring, UnityAction();public void AddListener(string key,UnityAction call){if (eventsDictionary.ContainsKey(key)){eventsDictionary[key] call;}elseeventsDictionary.Add(key, call);}/// summary/// 取消监听的命令/// /summarypublic 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 namecall/param/// returns/returnspublic 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 : SingletonPatternBaseEventCenterManager
{//键表示命令的名字//值表示命令具体要执行的逻辑Dictionarystring, IEventInfo eventsDictionary new Dictionarystring, 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 AddListenerT(object command,UnityActionT call){string key command.GetType().Name _ command.ToString() _ typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfoT).action call;}elseeventsDictionary.Add(key, new EventInfoT(call));}/// summary/// 取消监听的命令/// /summarypublic 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 RemoveListenerT(object command,UnityActionT call){string key command.GetType().Name _ command.ToString() _ typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfoT).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 RemoveListenersT(object command){string key command.GetType().Name _ command.ToString() _ typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfoT).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 DisPatchT(object command,T parameter){string key command.GetType().Name _ command.ToString() _ typeof(T).Name;if (eventsDictionary.ContainsKey(key)){(eventsDictionary[key] as EventInfoT).action?.Invoke(parameter);}}/// summary/// 移除所有带或者不带参数的监听事件,切换场景可以考虑使用/// /summarypublic void RemoveAllListeners(){eventsDictionary.Clear();}private interface IEventInfo { } //仅用于里氏替换原则private class EventInfo:IEventInfo{public UnityAction action;public EventInfo(UnityAction call){action call;}}private class EventInfoT :IEventInfo{public UnityActionT action;public EventInfo(UnityActionT call){action call;}}}传递多个参数的委托事件
可以写一个信息类来存储多个信息然后事件中传递这个信息类。这样就还是一个参数或者额外写含有两个参数的方法写含有三个的四个的可行但是费劲元组但是我不知道这个是什么