UGUI(四)事件系统的封装

时间:2022-10-12 15:15:05

UGUI的事件系统默认使用时,是需要当前的类继承事件接口,就可以在该类中加入它的事件方法,但是在多人开发时这种方式没什么好处。

用过NGUI的或许都知道UIEventListener这个类,因为使用超级方便,直接加入UI对象进行事件注册即可。其实UGUI也可以模仿这种方式进行封装。

代码如下:

  1. usingUnityEngine;
  2. usingSystem.Collections;
  3. usingUnityEngine.EventSystems;
  4. publicclassUUIEventListener
    : MonoBehaviour,
  5. IPointerClickHandler,
  6. IPointerDownHandler,
  7. IPointerEnterHandler,
  8. IPointerExitHandler,
  9. IPointerUpHandler,
  10. ISelectHandler,
  11. IUpdateSelectedHandler,
  12. IDeselectHandler,
  13. IDragHandler,
  14. IEndDragHandler,
  15. IDropHandler,
  16. IScrollHandler,
  17. IMoveHandler
  18. {
  19. publicdelegate
    voidVoidDelegate(GameObject
    go);
  20. publicVoidDelegate
    onClick;
  21. publicVoidDelegate
    onDown;
  22. publicVoidDelegate
    onEnter;
  23. publicVoidDelegate
    onExit;
  24. publicVoidDelegate
    onUp;
  25. publicVoidDelegate
    onSelect;
  26. publicVoidDelegate
    onUpdateSelect;
  27. publicVoidDelegate
    onDeSelect;
  28. publicVoidDelegate
    onDrag;
  29. publicVoidDelegate
    onDragEnd;
  30. publicVoidDelegate
    onDrop;
  31. publicVoidDelegate
    onScroll;
  32. publicVoidDelegate
    onMove;
  33. publicobject
    parameter;
  34. publicvoidOnPointerClick(PointerEventData
    eventData) { if(onClick
    != null)onClick(gameObject);
    }
  35. publicvoidOnPointerDown(PointerEventData
    eventData) { if(onDown
    != null)onDown(gameObject);
    }
  36. publicvoidOnPointerEnter(PointerEventData
    eventData) { if(onEnter
    != null)onEnter(gameObject);
    }
  37. publicvoidOnPointerExit(PointerEventData
    eventData) { if(onExit
    != null)onExit(gameObject);
    }
  38. publicvoidOnPointerUp(PointerEventData
    eventData) { if(onUp
    != null)onUp(gameObject);
    }
  39. publicvoidOnSelect(BaseEventData
    eventData) { if(onSelect
    != null)onSelect(gameObject);
    }
  40. publicvoidOnUpdateSelected(BaseEventData
    eventData) { if(onUpdateSelect
    != null)onUpdateSelect(gameObject);
    }
  41. publicvoidOnDeselect(BaseEventData
    eventData) { if(onDeSelect
    != null)onDeSelect(gameObject);
    }
  42. publicvoidOnDrag(PointerEventData
    eventData) { if(onDrag
    != null)onDrag(gameObject);
    }
  43. publicvoidOnEndDrag(PointerEventData
    eventData) { if(onDragEnd
    != null)onDragEnd(gameObject);
    }
  44. publicvoidOnDrop(PointerEventData
    eventData) { if(onDrop
    != null)onDrop(gameObject);
    }
  45. publicvoidOnScroll(PointerEventData
    eventData) { if(onScroll
    != null)onScroll(gameObject);
    }
  46. publicvoidOnMove(AxisEventData
    eventData) { if(onMove
    != null)onMove(gameObject);
    }
  47. staticpublicUUIEventListener
    Get(GameObject go)
  48. {
  49. UUIEventListenerlistener
    = go.GetComponent();
  50. if(listener
    == null)listener
    = go.AddComponent();
  51. returnlistener;
  52. }
  53. }

使用方式:

  1. UUIEventListener.Get(btn.gameObject).onClick=
    OnClickBtn;
  2. UUIEventListener.Get(btn.gameObject).parameter=
    index;
  3. privatevoidOnClickBtn(GameObject
    go)
  4. {
  5. intindex
    = (int)UUIEventListener.Get(go).parameter;
  6. Debug.Log("您单击的索引为:"+
    index);
  7. }