目录
归档
5501
2018 年 03 月 09 日
ET的动态添加事件和触发组件

功能就是在热更层里动态添加事件、触发等事件、使用起来也非常简单,代码也不多。

using ETModel;

using System; using UnityEngine;

using UnityEngine.Events;

using UnityEngine.EventSystems;

using UnityEngine.UI;

namespace ETHotfix

{

[ObjectSystem]

public class MonoEventComponentAwakeSystem : AwakeSystem<MonoEventComponent>

{

public override void Awake(MonoEventComponent self)

{

self.Awake();

}

}

public class MonoEventComponent: Component

{

public static MonoEventComponent Instance;

public void Awake()

{

Instance = this;

}

public void AddButtonClick(Button obj, Action action)

{

AddButtonClick(obj, true, action);

}

/// <summary> /// 添加ButtonClick事件 /// </summary>

/// <param name=”obj”>Button组件</param>

/// <param name=”IsRemoveAllListeners”>是否清除以前注册的事件</param>

/// <param name=”action”>回调函数</param>

public void AddButtonClick(Button obj, bool IsRemoveAllListeners, Action action)

{

if (IsRemoveAllListeners) obj.onClick.RemoveAllListeners(); obj.onClick.Add(action);

}

/// <summary> /// 添加触发事件 /// </summary>

/// <param name=”obj”>GameObject</param>

/// <param name=”eventTriggerType”>EventTriggerType</param>

/// <param name=”action”>回调函数</param>

/// <returns></returns>

public EventTrigger AddEventTrigger(GameObject obj, EventTriggerType eventTriggerType, Action<BaseEventData> action)

{

return AddEventTrigger(obj, eventTriggerType, true, action);

}

/// <summary> /// 添加触发事件 /// </summary>

/// <param name=”obj”>GameObject</param>

/// <param name=”eventTriggerType”>EventTriggerType</param>

/// <param name=”IsRemoveAllListeners”>添加事件是否清空以前添加的事件</param>

/// <param name=”action”>回调函数</param>

/// <returns></returns>

public EventTrigger AddEventTrigger( GameObject obj, EventTriggerType eventTriggerType, bool IsRemoveAllListeners, Action<BaseEventData> action)

{

return AddEventTrigger(obj, eventTriggerType, IsRemoveAllListeners, false, action);

}

/// <summary> /// 添加触发事件 /// </summary>

/// <param name=”obj”>GameObject</param>

/// <param name=”eventTriggerType”>EventTriggerType</param>

/// <param name=”IsRemoveAllListeners”>添加事件是否清空以前添加的事件</param>

/// <param name=”IsClearTriggers”>是否清除GameObject上所有触发事件</param>

/// <param name=”action”>回调函数</param>

/// <returns></returns>

public EventTrigger AddEventTrigger( GameObject obj, EventTriggerType eventTriggerType, bool IsRemoveAllListeners, bool IsClearTriggers, Action<BaseEventData> action)

{

if (obj == null) return null;

EventTrigger eventTrigger = obj.GetComponent<EventTrigger>() ?? obj.AddComponent<EventTrigger>();

EventTrigger.Entry entry = new EventTrigger.Entry { eventID = eventTriggerType };

UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action); entry.callback.AddListener(callback); if (IsClearTriggers) eventTrigger.triggers.Clear();

eventTrigger.triggers.Add(entry); return eventTrigger;

}

}

}

转载原创文章请注明,转载自: Tinkingli博客 » ET的动态添加事件和触发组件
1条评论
Loading...
  • […] 群友分享: ET学习笔记系列(咲夜詩写) 框架服务端运行流程 ET启动配置 框架demo介绍 linux部署 linux部署,mongo安装,资源服搭建 ET框架心跳包组件开发 ET框架Actor使用与心得 基于ET框架和UGUI的简单UI框架实现(渐渐写) ET框架笔记 (笑览世界写) ET框架如何用MAC开发 ET的动态添加事件和触发组件 […]

发表评论

电子邮件地址不会被公开。 必填项已用*标注