298 lines
7.7 KiB
C#
298 lines
7.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using System;
|
|
using DG.Tweening;
|
|
using UnityEngine.UI;
|
|
|
|
namespace BF
|
|
{
|
|
public enum UITouchEventType
|
|
{
|
|
Unknown = 0,
|
|
Down,
|
|
Drag,
|
|
UpInside,
|
|
UpOutside,
|
|
DragCancelUp,
|
|
Cancel,
|
|
DragUp,
|
|
DragDown,
|
|
DragLeft,
|
|
DragRight,
|
|
}
|
|
|
|
[RequireComponent(typeof(Graphic))]
|
|
public class UITouchEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler, ICancelHandler
|
|
{
|
|
protected Action<int, float, float> luaFunc;
|
|
[Header("是否播放点击动画")]
|
|
public bool IsShowClickAnimation = true;
|
|
private bool animationStart = false;
|
|
private static float touchBeginScale = 0.95f;
|
|
private Vector3 originScale;
|
|
private Sequence sequence;
|
|
private bool touchInsideButton = false;
|
|
protected int currentPointerId = 0;
|
|
private bool touching = false;
|
|
protected bool enable = true;
|
|
|
|
public static bool disableClickEvent = false;
|
|
|
|
#if UNITY_EDITOR
|
|
private UITouchEventType touchEvent = UITouchEventType.Unknown;
|
|
#endif
|
|
public void AddTouchEventListener(Action<int, float, float> func)
|
|
{
|
|
luaFunc = func;
|
|
|
|
}
|
|
|
|
public void RemoveEventListener()
|
|
{
|
|
luaFunc = null;
|
|
}
|
|
|
|
public void SetShowClickAnimation(bool isShow)
|
|
{
|
|
IsShowClickAnimation = isShow;
|
|
}
|
|
|
|
public bool GetShowClickAnimation()
|
|
{
|
|
return IsShowClickAnimation;
|
|
}
|
|
|
|
public void SetTouchEnable(bool enable)
|
|
{
|
|
this.enable = enable;
|
|
if (enable)
|
|
{
|
|
touching = false;
|
|
}
|
|
}
|
|
|
|
public void StopTouchAnimation()
|
|
{
|
|
if (sequence != null && sequence.IsPlaying())
|
|
{
|
|
sequence.Pause();
|
|
if (!ReferenceEquals(originScale, null))
|
|
{
|
|
this.transform.localScale = originScale;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
if (touching)
|
|
{
|
|
return;
|
|
}
|
|
#if UNITY_EDITOR
|
|
touchEvent = UITouchEventType.Down;
|
|
#endif
|
|
touching = true;
|
|
currentPointerId = eventData.pointerId;
|
|
touchInsideButton = true;
|
|
PlayTouchBegin();
|
|
if (luaFunc != null)
|
|
{
|
|
luaFunc((int)UITouchEventType.Down, eventData.position.x, eventData.position.y);
|
|
}
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
if (currentPointerId != eventData.pointerId)
|
|
{
|
|
return;
|
|
}
|
|
#if UNITY_EDITOR
|
|
if (touchInsideButton)
|
|
{
|
|
touchEvent = UITouchEventType.UpInside;
|
|
}
|
|
else
|
|
{
|
|
touchEvent = UITouchEventType.UpOutside;
|
|
}
|
|
#endif
|
|
PlayTouchEnd();
|
|
|
|
if (luaFunc != null)
|
|
{
|
|
if (!eventData.dragging)
|
|
{
|
|
if (touchInsideButton)
|
|
{
|
|
if (!disableClickEvent)
|
|
{
|
|
luaFunc((int)UITouchEventType.UpInside, eventData.position.x, eventData.position.y);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
luaFunc((int)UITouchEventType.UpOutside, eventData.position.x, eventData.position.y);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
luaFunc((int)UITouchEventType.DragCancelUp, eventData.position.x, eventData.position.y);
|
|
}
|
|
}
|
|
touching = false;
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
if (currentPointerId != eventData.pointerId)
|
|
{
|
|
return;
|
|
}
|
|
if (!ReferenceEquals(eventData.pointerPress, eventData.pointerEnter))
|
|
{
|
|
return;
|
|
}
|
|
#if UNITY_EDITOR
|
|
if (touchEvent != UITouchEventType.Down)
|
|
{
|
|
return;
|
|
}
|
|
#endif
|
|
touchInsideButton = true;
|
|
PlayTouchBegin();
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
if (currentPointerId != eventData.pointerId)
|
|
{
|
|
return;
|
|
}
|
|
if (!ReferenceEquals(eventData.pointerPress, eventData.pointerEnter))
|
|
{
|
|
return;
|
|
}
|
|
#if UNITY_EDITOR
|
|
if (touchEvent != UITouchEventType.Down)
|
|
{
|
|
return;
|
|
}
|
|
#endif
|
|
touchInsideButton = false;
|
|
PlayTouchEnd();
|
|
}
|
|
|
|
public void OnCancel(BaseEventData eventData)
|
|
{
|
|
if (!enable)
|
|
{
|
|
return;
|
|
}
|
|
#if UNITY_EDITOR
|
|
touchEvent = UITouchEventType.Cancel;
|
|
#endif
|
|
PlayTouchEnd();
|
|
if (luaFunc != null)
|
|
{
|
|
luaFunc((int)UITouchEventType.Cancel, 0.0f, 0.0f);
|
|
}
|
|
touching = false;
|
|
}
|
|
|
|
private void PlayTouchBegin()
|
|
{
|
|
if (!IsShowClickAnimation)
|
|
{
|
|
return;
|
|
}
|
|
if (animationStart)
|
|
{
|
|
return;
|
|
}
|
|
animationStart = true;
|
|
if (sequence != null && sequence.IsPlaying())
|
|
{
|
|
sequence.Pause();
|
|
if (!ReferenceEquals(originScale, null))
|
|
{
|
|
this.transform.localScale = originScale;
|
|
}
|
|
}
|
|
if (originScale != this.transform.localScale)
|
|
{
|
|
originScale = this.transform.localScale;
|
|
if (sequence != null)
|
|
{
|
|
sequence.Kill();
|
|
sequence = null;
|
|
}
|
|
}
|
|
this.transform.localScale = originScale * touchBeginScale;
|
|
}
|
|
|
|
private void PlayTouchEnd()
|
|
{
|
|
if (!IsShowClickAnimation)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!animationStart)
|
|
{
|
|
return;
|
|
}
|
|
|
|
animationStart = false;
|
|
if (sequence == null)
|
|
{
|
|
if (ReferenceEquals(originScale, null))
|
|
{
|
|
originScale = this.transform.localScale;
|
|
}
|
|
sequence = DOTween.Sequence().Append(
|
|
transform.DOScale(originScale * 1.05f, 0.08f).SetEase(Ease.InOutSine)
|
|
).Append(
|
|
transform.DOScale(originScale * 0.96f, 0.08f).SetEase(Ease.InOutSine)
|
|
).Append(
|
|
transform.DOScale(originScale, 0.08f).SetEase(Ease.InOutSine)
|
|
);
|
|
sequence.SetAutoKill(false);
|
|
}
|
|
else
|
|
{
|
|
sequence.Restart();
|
|
}
|
|
}
|
|
|
|
void OnDestroy()
|
|
{
|
|
luaFunc = null;
|
|
if (sequence != null)
|
|
{
|
|
sequence.Kill();
|
|
sequence = null;
|
|
}
|
|
}
|
|
}
|
|
}
|