案例:Assets/Scripts/Example/3.UIFrame/UIEvent/UIEventExample.unity

1).UI脚本注册事件

UIMainEvent脚本RegisterUIEvent函数 :

  1. protected override void RegisterUIEvent()
  2. {
  3. trans.onClick.AddListener(()=>
  4. {
  5. UIManager.Instance.OpenUI<UIChildEvent>((int)eventUITag.UIChildEvent, null, panelLayer: PanelLayer.Load, isClear: true);
  6. });
  7. RegisterEvent((int)eventID.changeMainColor, ChangeColor);
  8. }
  9. //事件响应
  10. private void ChangeColor(int key,MsgInfo param)
  11. {
  12. if (param != null && param.DataList.Count > 0)
  13. {
  14. Color color = (Color)param.DataList[0];
  15. bg.color = color;
  16. }
  17. }

2).发送UI消息

这个案例时打开子面板然后点击子面板按钮改变父面板背景颜色
子面板UIChildEvent :

  1. protected override void RegisterUIEvent()
  2. {
  3. button.onClick.AddListener(() =>
  4. {
  5. SendMsg((int)eventID.changeMainColor,new MsgInfo(Color.red));
  6. });
  7. }