public static void OnClick(this Transform trans,QVoidDelegate.WithVoid callback)
		{
			QFramework.UI.UGUIEventListener.Get (trans.gameObject);

			var listener = QFramework.UI.UGUIEventListener.CheckAndAddListener (trans.gameObject);
			listener.onClick += callback;
		}
		public static void OnClick(this MonoBehaviour behaviour,QVoidDelegate.WithVoid callback)
		{
			QFramework.UI.UGUIEventListener.Get (behaviour.gameObject);

			var listener = QFramework.UI.UGUIEventListener.CheckAndAddListener (behaviour.gameObject);
			listener.onClick += callback;
		}
Beispiel #3
0
		public static void OnClick(this GameObject go,QVoidDelegate.WithVoid callback)
		{
			QFramework.UI.UGUIEventListener.Get (go);

			var listener = QFramework.UI.UGUIEventListener.CheckAndAddListener (go);
			listener.onClick += callback;
		}
		/// <summary>
		/// 注册消息,
		/// 注意第一个参数,使用了C# this的扩展,
		/// 所以只有实现IMsgReceiver的对象才能调用此方法
		/// </summary>
		public static void RegisterGlobalMsg(this IMsgReceiver self, string msgName,QVoidDelegate.WithParams callback)
		{
			// 略过
			if (string.IsNullOrEmpty(msgName)) {
				Debug.LogError("RegisterMsg:" + msgName + " is Null or Empty");
				return;
			}

			// 略过
			if (null == callback) {
				Debug.LogError ("RegisterMsg:" + msgName + " callback is Null");
				return;
			}
				
			// 添加消息通道
			if (!mMsgHandlerDict.ContainsKey (QMsgChannel.Global)) {
				mMsgHandlerDict [QMsgChannel.Global] = new Dictionary<string, List<QMsgHandler>> ();
			}

			// 略过
			if (!mMsgHandlerDict[QMsgChannel.Global].ContainsKey (msgName)) {
				mMsgHandlerDict[QMsgChannel.Global] [msgName] = new List<QMsgHandler> ();
			}

			// 看下这里
			var handlers = mMsgHandlerDict [QMsgChannel.Global][msgName];

			// 略过
			// 防止重复注册
			foreach (var handler in handlers) {
				if (handler.receiver == self && handler.callback == callback) {
					Debug.LogWarning ("RegisterMsg:" + msgName + " ayready Register");
					return;
				}
			}

			// 再看下这里
			handlers.Add (new QMsgHandler (self, callback));
		}
		public static void UnRegisterMsg(this IMsgReceiver self,string msgName,QVoidDelegate.WithParams callback,QMsgChannel channel = QMsgChannel.Global)
		{
			if (CheckStrNullOrEmpty (msgName) || CheckDelegateNull(callback)) {
				return;
			}
				
			// 添加通道
			if (!mMsgHandlerDict.ContainsKey (channel)) {
				Debug.LogError ("Channel:" + channel.ToString () + " doesn't exist");
				return;
			}

			var handlers = mMsgHandlerDict [channel] [msgName];

			int handlerCount = handlers.Count;

			// 删除List需要从后向前遍历
			for (int index = handlerCount - 1; index >= 0; index--) {
				var handler = handlers [index];
				if (handler.receiver == self && handler.callback == callback) {
					handlers.Remove (handler);
					break;
				}
			}
		}
			/*
			 * VoidDelegate.WithParams 是一种委托 ,定义是这样的 
			 * 
			 *  public class VoidDelegate{
			 *  	public delegate void WithParams(params object[] paramList);
			 *  }
			 */
			public QMsgHandler(IMsgReceiver receiver,QVoidDelegate.WithParams callback)
			{
				this.receiver = receiver;
				this.callback = callback;
			}
		public static void RegisterLogicMsg(this IMsgReceiver self, string msgName,QVoidDelegate.WithParams callback,QMsgChannel channel = QMsgChannel.Global)
		{
			if (CheckStrNullOrEmpty (msgName)||CheckDelegateNull(callback)) {
				return;
			}

			// 添加消息通道
			if (!mMsgHandlerDict.ContainsKey (channel)) {
				mMsgHandlerDict [channel] = new Dictionary<string, List<QMsgHandler>> ();
			}

			// 略过
			if (!mMsgHandlerDict[channel].ContainsKey (msgName)) {
				mMsgHandlerDict[channel] [msgName] = new List<QMsgHandler> ();
			}

			// 看下这里
			var handlers = mMsgHandlerDict [channel][msgName];

			// 略过
			// 防止重复注册
			foreach (var handler in handlers) {
				if (handler.receiver == self && handler.callback == callback) {
					Debug.LogWarning ("RegisterMsg:" + msgName + " ayready Register");
					return;
				}
			}

			// 再看下这里
			handlers.Add (new QMsgHandler (self, callback));
		}
		static bool CheckDelegateNull(QVoidDelegate.WithParams callback)
		{
			if (null == callback) {
				Debug.LogWarning ("callback is Null");

				return true;
			}
			return false;
		}