1.在调用委托时报错KeyNotFoundException: Cannot find Delegate Adapter for:HotFix_Project.ui_page_logingame.Go
代码如下:在hotfix中
protected override void _PageOpenImpl() { UnityEngine.Debug.Log("11111111111111111111111111111111111111111111"); ObserverManager.RegisterNotification(NotificationConstant.Login_Succ, Go); }
主工程中ObserverManager.cs
using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using CSharpEngine.Artifact; public delegate void NotificationAction(EventParam param); public class ObserverManager { // 观察者通知池 private static Dictionary<string, List<NotificationAction>> _notifierDict = new Dictionary<string, List<NotificationAction>>(); /// <summary> /// 注册观察者通知 /// </summary> public static void RegisterNotification(string _note,NotificationAction _obs) { if (!_notifierDict.ContainsKey(_note)) { _notifierDict.Add(_note,new List<NotificationAction>()); } _notifierDict[_note].Add(_obs); } /// <summary> /// 撤销观察者通知 /// </summary> public static void UnregisterNotification(string _note, NotificationAction _obs) { if (_notifierDict.ContainsKey(_note)) { for (int i = 0; i < _notifierDict[_note].Count; i++) { if (_notifierDict[_note][i] == _obs) { _notifierDict[_note].RemoveAt(i); break; } } } } internal static void RegisterNotification(object setReady) { throw new NotImplementedException(); } /// <summary> /// 发送信息 /// </summary> public static void SendNotification(string _note, EventParam _param) { if (_notifierDict.ContainsKey(_note)) { for (int i = 0; i < _notifierDict[_note].Count; i++) { _notifierDict[_note][i]?.Invoke(_param); } } } /// <summary> /// 发送信息 /// </summary> public static void SendNotificationC(string note_, EventParam data_) { SendNotification(note_, data_); } }
其中用到了
EventParam 和
NotificationAction必须在主工程注册为委托才行。
注册代码如下:
manager.RegisterMethodDelegate<CSharpEngine.Artifact.EventParam>();
manager.RegisterMethodDelegate<NotificationAction>();
注册后调用委托又报其它错:
这时要添加Notification的转换器才行
在主工程代码如下
appdomain.DelegateManager.RegisterDelegateConvertor<NotificationAction>((action) => { return new NotificationAction((o) => { ((System.Action)action)(); }); });
OK 跑过了!!!
参考文章:https://www.cnblogs.com/bbdr/articles/14303840.html
然后发送观察者的时候又发现报错:

错误代码为:
将其修改为:

好了,这次真的可以了。