想将EventSystem封装一下,手机端触控操作的手势合并一下 相关一些介绍以及思路在我的这篇文字里面:EventSystem
总的事件分配管理
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIEventMgr : MonoBehaviour { #region PointerEvent /// <summary> /// Enter /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddEnter(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<PointerEnterHandler>() ?? obj.AddComponent<PointerEnterHandler>(); handle.callback += func; } public static void RemoveEnter(GameObject obj) { var handle = obj.GetComponent<PointerEnterHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// Exit /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddExit(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<PointerExitHandler>() ?? obj.AddComponent<PointerExitHandler>(); handle.callback += func; } public static void RemoveExit(GameObject obj) { var handle = obj.GetComponent<PointerExitHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// Down /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDown(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<PointerDownHandler>() ?? obj.AddComponent<PointerDownHandler>(); handle.callback += func; } public static void RemoveDown(GameObject obj) { var handle = obj.GetComponent<PointerDownHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// Up /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddUp(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<PointerUpHandler>() ?? obj.AddComponent<PointerUpHandler>(); handle.callback += func; } public static void RemoveUp(GameObject obj) { var handle = obj.GetComponent<PointerUpHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion #region ClickEvent /// <summary> /// Click /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddClick(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<PointerClickHandler>() ?? obj.AddComponent<PointerClickHandler>(); handle.callback += func; } public static void RemoveClick(GameObject obj) { var handle = obj.GetComponent<PointerClickHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// DoubleClick /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDoubleClick(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<DoubleClickHandle>() ?? obj.AddComponent<DoubleClickHandle>(); handle.callback += func; } public static void RemoveDoubleClick(GameObject obj) { var handle = obj.GetComponent<DoubleClickHandle>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion #region LongPressEvent /// <summary> /// LongPress /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddLongPress(GameObject obj, DeleGameObject func) { AddLongPress(obj, 2, func); } public static void AddLongPress(GameObject obj, float timeInterval,DeleGameObject func) { var handle = obj.GetComponent<PointerLongPressHandle>() ?? obj.AddComponent<PointerLongPressHandle>(); handle.callback += func; handle.timeInterval = timeInterval; } public static void RemoveLongPress(GameObject obj) { var handle = obj.GetComponent<PointerLongPressHandle>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion #region ZoomEvent /// <summary> /// Zoom /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddZoom(GameObject obj, DeleFloat func) { AddZoom(obj,1, func); } public static void AddZoom(GameObject obj, float scaleFactor, DeleFloat func) { var handle = obj.GetComponent<ZoomHandle>() ?? obj.AddComponent<ZoomHandle>(); handle.callback += func; handle.factor = scaleFactor; } public static void RemoveZoom(GameObject obj) { var handle = obj.GetComponent<ZoomHandle>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion #region DragEvent /// <summary> /// DragBegin /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDragBegin(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<DragBeginHandler>() ?? obj.AddComponent<DragBeginHandler>(); handle.callback += func; } public static void RemoveDragBegin(GameObject obj) { var handle = obj.GetComponent<DragBeginHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// Drag /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDrag(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<DragHandler>() ?? obj.AddComponent<DragHandler>(); handle.callback += func; } public static void RemoveDrag(GameObject obj) { var handle = obj.GetComponent<DragHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } /// <summary> /// DragEnd /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDragEnd(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<DragEndHandler>() ?? obj.AddComponent<DragEndHandler>(); handle.callback += func; } public static void RemoveDragEnd(GameObject obj) { var handle = obj.GetComponent<DragEndHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion #region DropEvent /// <summary> /// Drop /// </summary> /// <param name="obj"></param> /// <param name="func"></param> public static void AddDrop(GameObject obj, DeleGameObject func) { var handle = obj.GetComponent<DropHandler>() ?? obj.AddComponent<DropHandler>(); handle.callback += func; } public static void RemoveDrop(GameObject obj) { var handle = obj.GetComponent<DropHandler>(); if (handle == null) return; handle.callback = null; Destroy(handle); } #endregion }委托定义
using System.Collections; using System.Collections.Generic; using UnityEngine; public delegate void DeleGameObject(GameObject obj); public delegate void DeleFloat(float f); public delegate void DeleBool(bool b); public delegate void DeleInt(int i); public delegate void DeleFloatObj(GameObject obj,float f); public delegate void DeleIntObj(GameObject obj,int i);进入事件
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class PointerEnterHandler : MonoBehaviour, IPointerEnterHandler { public DeleGameObject callback; public void OnPointerEnter(PointerEventData eventData) { callback?.Invoke(eventData.pointerEnter); } }事件处理
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class cube : MonoBehaviour { void Start () { UIEventMgr.AddDoubleClick(gameObject, OnDoubleClick); UIEventMgr.AddClick(gameObject, OnClick); //Invoke("RemoveEvent", 5); } private void OnDoubleClick(GameObject obj) { Debug.LogError(obj.name); } void RemoveEvent() { UIEventMgr.RemoveClick(gameObject); UIEventMgr.RemoveDoubleClick(gameObject); } private void OnClick(GameObject obj) { Debug.LogError("cube_click"); } }需要多添加一个层级就多加一个层就好