简介
想将EventSystem封装一下,手机端触控操作的手势合并一下
相关一些介绍以及思路在我的这篇文字里面:EventSystem
UIEventMgr
总的事件分配管理
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIEventMgr : MonoBehaviour {
#region PointerEvent
///
/// Enter
///
///
///
public static void AddEnter(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveEnter(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// Exit
///
///
///
public static void AddExit(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveExit(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// Down
///
///
///
public static void AddDown(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDown(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// Up
///
///
///
public static void AddUp(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveUp(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
#region ClickEvent
///
/// Click
///
///
///
public static void AddClick(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveClick(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// DoubleClick
///
///
///
public static void AddDoubleClick(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDoubleClick(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
#region LongPressEvent
///
/// LongPress
///
///
///
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() ?? obj.AddComponent();
handle.callback += func;
handle.timeInterval = timeInterval;
}
public static void RemoveLongPress(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
#region ZoomEvent
///
/// Zoom
///
///
///
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() ?? obj.AddComponent();
handle.callback += func;
handle.factor = scaleFactor;
}
public static void RemoveZoom(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
#region DragEvent
///
/// DragBegin
///
///
///
public static void AddDragBegin(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDragBegin(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// Drag
///
///
///
public static void AddDrag(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDrag(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
///
/// DragEnd
///
///
///
public static void AddDragEnd(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDragEnd(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
#region DropEvent
///
/// Drop
///
///
///
public static void AddDrop(GameObject obj, DeleGameObject func)
{
var handle = obj.GetComponent() ?? obj.AddComponent();
handle.callback += func;
}
public static void RemoveDrop(GameObject obj)
{
var handle = obj.GetComponent();
if (handle == null) return;
handle.callback = null;
Destroy(handle);
}
#endregion
}
Delegates
委托定义
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);
PointerEnterHandle
进入事件
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);
}
}
cube
事件处理
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");
}
}
需要多添加一个层级就多加一个层就好