/// <summary> /// 移除指定监听 /// </summary> /// <param name="eventType">事件类型</param> /// <param name="sel">要移除的监听</param> /// <returns>是否移除成功</returns> public bool removeEventListener(string eventType, SFListenerSelector sel) { if (hasEventListener(eventType, sel)) { var pairs = m_dictListeners[eventType]; foreach (var pair in pairs) { if (pair.selector == sel) { pairs.Remove(pair); return(true); } } } return(false); }
/// <summary> /// 检查是否已经添加了某事件的某监听 /// </summary> /// <param name="eventType">事件类型</param> /// <param name="sel">需要检查的监听</param> /// <returns>是否已经添加</returns> public bool hasEventListener(string eventType, SFListenerSelector sel) { if (m_dictListeners.ContainsKey(eventType)) { List <SFPairOfListenerAndSelector> selectors = m_dictListeners[eventType]; SFPairOfListenerAndSelector target = selectors.Find(delegate(SFPairOfListenerAndSelector src) { return(sel.GetHashCode() == src.selector.GetHashCode()); }); if (target != null) { return(true); } } return(false); }
/// <summary> /// 添加一个监听 /// </summary> /// <param name="eventType">事件类型</param> /// <param name="sel">需要添加的监听</param> /// <returns>是否添加成功</returns> public bool addEventListener(object listener, string eventType, SFListenerSelector sel) { if (eventType != "" && sel != null) // 判断有效性 { if (hasEventListener(eventType, sel)) { SFUtils.logWarning(string.Format("重复监听!type={0}", eventType)); } if (!m_dictListeners.ContainsKey(eventType)) { // 不存在的话就新建一个 List <SFPairOfListenerAndSelector> newSelectors = new List <SFPairOfListenerAndSelector>(); m_dictListeners[eventType] = newSelectors; } var selectors = m_dictListeners[eventType]; var pair = new SFPairOfListenerAndSelector(listener, sel); selectors.Add(pair); return(true); } return(false); }
public SFPairOfListenerAndSelector(object _listener, SFListenerSelector _selector) { listener = _listener; selector = _selector; }
/// <summary> /// 添加UI事件监听 /// </summary> /// <param name="widget">UI组件</param> /// <param name="eventType">事件类型</param> /// <param name="sel">回调函数</param> public void addEventListener(Component widget, string eventType, SFListenerSelector sel) { var dispatcher = SFUIEventListener.getDispatcherWithGo(widget.gameObject); dispatcher.addEventListener(this, eventType, sel); }