You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.2 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;
public class TouchArea : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerExitHandler, IPointerUpHandler
{
[HideInInspector]
public Image img;
public bool MouseOnly = false;
[System.Serializable]
public class OnPointerDownEvent : UnityEvent<PointerEventData> { };
public OnPointerDownEvent PointerDown = new OnPointerDownEvent();
[System.Serializable]
public class OnPointerDragEvent : UnityEvent<PointerEventData> { };
public OnPointerDragEvent PointerDrag = new OnPointerDragEvent();
[System.Serializable]
public class OnPointerUpEvent : UnityEvent<PointerEventData> { };
public OnPointerUpEvent PointerUp = new OnPointerUpEvent();
[System.Serializable]
public class OnPointerExitEvent : UnityEvent<PointerEventData> { };
public OnPointerExitEvent PointerExit = new OnPointerExitEvent();
private void Awake()
{
img = GetComponent<Image>();
}
public void OnPointerDown(PointerEventData eventData)
{
if (PointerDown == null)
return;
if (skip_on_mouse_only(eventData))
return;
PointerDown.Invoke(eventData);
//Debug.Log("OnPointerDown");
}
public void OnDrag(PointerEventData eventData)
{
if (PointerDrag == null)
return;
if (skip_on_mouse_only(eventData))
return;
PointerDrag.Invoke(eventData);
//Debug.Log("OnDrag");
}
public void OnPointerUp(PointerEventData eventData)
{
if (PointerUp == null)
return;
if (skip_on_mouse_only(eventData))
return;
PointerUp.Invoke(eventData);
//Debug.Log("OnPointerUp");
}
public void OnPointerExit(PointerEventData eventData)
{
if (PointerExit == null)
return;
if (skip_on_mouse_only(eventData))
return;
PointerExit.Invoke(eventData);
//Debug.Log("OnPointerExit");
}
private bool skip_on_mouse_only(PointerEventData eventData)
{
return MouseOnly && eventData.pointerId != -1;
}
}