using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace WXUtils.UI
{
public class WXListener : MonoBehaviour,
IPointerClickHandler,
IPointerDownHandler,
IPointerUpHandler,
IBeginDragHandler,
IDragHandler,
IEndDragHandler
{
public Action<PointerEventData , object []> onClick;
public Action<PointerEventData , object []> onClickDown;
public Action<PointerEventData , object []> onClickUp;
public Action<PointerEventData , object []> onBeginDrag;
public Action<PointerEventData , object []> onDrag;
public Action<PointerEventData , object []> onEndDrag;
public object [] args = null;
public void OnPointerClick(PointerEventData eventData) {
onClick?.Invoke(eventData , args);
}
public void OnBeginDrag(PointerEventData eventData) {
onBeginDrag?.Invoke(eventData , args);
}
public void OnDrag(PointerEventData eventData) {
onDrag?.Invoke(eventData , args);
}
public void OnEndDrag(PointerEventData eventData) {
onEndDrag?.Invoke(eventData , args);
}
public void OnPointerDown(PointerEventData eventData) {
onClickDown?.Invoke(eventData , args);
}
public void OnPointerUp(PointerEventData eventData) {
onClickUp?.Invoke(eventData , args);
}
}
}
using System;
using UnityEngine;
using UnityEngine.EventSystems;
namespace WXUtils.UI
{
public static class UIExtension
{
public static void OnClick(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClick = action;
if (null != args) {
listener.args = args;
}
}
public static void OnClick(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClick = action;
if (null != args) {
listener.args = args;
}
}
public static void OnClickDown(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClickDown = action;
if (null != args) {
listener.args = args;
}
}
public static void OnClickDown(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClickDown = action;
if (null != args) {
listener.args = args;
}
}
public static void OnClickUp(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClickUp = action;
if (null != args) {
listener.args = args;
}
}
public static void OnClickUp(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onClickUp = action;
if (null != args) {
listener.args = args;
}
}
public static void OnBeginDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onBeginDrag = action;
if (null != args) {
listener.args = args;
}
}
public static void OnBeginDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onBeginDrag = action;
if (null != args) {
listener.args = args;
}
}
public static void OnDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onDrag = action;
if (null != args) {
listener.args = args;
}
}
public static void OnDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onDrag = action;
if (null != args) {
listener.args = args;
}
}
public static void OnEndDrag(this GameObject go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onEndDrag = action;
if (null != args) {
listener.args = args;
}
}
public static void OnEndDrag(this Transform go , Action<PointerEventData , object []> action , params object [] args) {
WXListener listener = go.GetOrAddComponent<WXListener>();
listener.onEndDrag = action;
if (null != args) {
listener.args = args;
}
}
}
public static class Utils
{
public static T GetOrAddComponent<T>(this GameObject go) where T : Component {
T t = go.GetComponent<T>();
if (null == t) {
t = go.AddComponent<T>();
}
return t;
}
public static T GetOrAddComponent<T>(this Transform go) where T : Component {
T t = go.GetComponent<T>();
if (null == t) {
t = go.gameObject.AddComponent<T>();
}
return t;
}
}
}
using UnityEngine;
using UnityEngine.EventSystems;
using WXUtils.UI;
public class Test : MonoBehaviour
{
public GameObject go;
void Start()
{
go.OnClick(Click,go);
go.OnClickDown(ClickDown , go);
go.OnClickUp(ClickUp , go);
go.OnBeginDrag(OnBeginDrag , go);
go.OnEndDrag(OnEndDrag , go);
go.OnDrag(Drag , go);
}
private void Click(PointerEventData data,object[]args) {
if (offset==Vector2.zero) {
Debug.Log((args [0] as GameObject).name+"-- Click");
}
}
private void ClickDown(PointerEventData data , object [] args) {
Debug.Log((args [0] as GameObject).name+"-- ClickDown");
}
private void ClickUp(PointerEventData data , object [] args) {
Debug.Log((args [0] as GameObject).name + "-- ClickUp");
}
Vector2 offset;
private void OnBeginDrag(PointerEventData data , object [] args) {
GameObject value = args [0] as GameObject;
offset = (Vector2)value.transform.position - data.position;
}
private void Drag(PointerEventData data , object [] args) {
GameObject value = args [0] as GameObject;
value.transform.position = data.position+offset;
}
private void OnEndDrag(PointerEventData data , object [] args) {
Debug.Log("EndDrag");
offset = Vector2.zero;
}
}