using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.EventSystems;
|
public class TreasureModelDrag : MonoBehaviour, IBeginDragHandler, IEndDragHandler, IPointerDownHandler,
|
IPointerUpHandler, IDragHandler, IPointerClickHandler
|
{
|
Vector2 m_Start;
|
bool m_Drag = false;
|
|
public event Action<float> OnValueChange;
|
public event Action OnDragComplete;
|
public event Action OnDragBegine;
|
|
public void OnBeginDrag(PointerEventData eventData)
|
{
|
m_Start = eventData.position;
|
m_Drag = true;
|
if (OnDragBegine != null)
|
{
|
OnDragBegine();
|
}
|
}
|
|
public void OnDrag(PointerEventData eventData)
|
{
|
if (m_Drag)
|
{
|
var _delta = eventData.position - m_Start;
|
m_Start = eventData.position;
|
if (OnValueChange != null)
|
{
|
OnValueChange(_delta.x);
|
}
|
}
|
}
|
|
public void OnEndDrag(PointerEventData eventData)
|
{
|
if (m_Drag && OnDragComplete != null)
|
{
|
OnDragComplete();
|
}
|
m_Drag = false;
|
}
|
|
public void OnPointerClick(PointerEventData eventData)
|
{
|
if (m_Drag)
|
{
|
return;
|
}
|
PassEvent(eventData, ExecuteEvents.pointerClickHandler);
|
}
|
|
public void OnPointerDown(PointerEventData eventData)
|
{
|
PassEvent(eventData, ExecuteEvents.pointerDownHandler);
|
}
|
|
public void OnPointerUp(PointerEventData eventData)
|
{
|
if (m_Drag && OnDragComplete != null)
|
{
|
OnDragComplete();
|
}
|
m_Drag = false;
|
PassEvent(eventData, ExecuteEvents.pointerUpHandler);
|
}
|
|
void PassEvent<T>(PointerEventData data, ExecuteEvents.EventFunction<T> function) where T : IEventSystemHandler
|
{
|
List<RaycastResult> results = new List<RaycastResult>();
|
EventSystem.current.RaycastAll(data, results);
|
GameObject current = data.pointerCurrentRaycast.gameObject;
|
for (int i = 0; i < results.Count; i++)
|
{
|
if (current != results[i].gameObject)
|
{
|
ExecuteEvents.Execute(results[i].gameObject, data, function);
|
}
|
}
|
}
|
}
|