zoukankan      html  css  js  c++  java
  • UGUI(四)事件系统的封装

    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. }
  • 相关阅读:
    递归的效率问题以及递归与循环的比较
    malloc/free与new/delete的区别
    类的综合案例——纯虚函数与抽象类( 加强对接口与多态,以及派生类构造函数的理解 )
    对象移动、右值引用详解
    深入理解类成员函数的调用规则(理解成员函数的内存为什么不会反映在sizeof运算符上、类的静态绑定与动态绑定、虚函数表)
    为什么NULL指针也能访问成员函数?(但不能访问成员变量)
    洛谷P1656 炸铁路
    Vijos1901 学姐的钱包
    洛谷P2327 [SCOI2005] 扫雷
    洛谷P1993 小 K 的农场
  • 原文地址:https://www.cnblogs.com/nafio/p/9137499.html
Copyright © 2011-2022 走看看