少年修仙传客户端代码仓库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class UGUIEventListenerContainDrag : MonoBehaviour,
                            IPointerClickHandler,
                            IPointerDownHandler,
                            IPointerEnterHandler,
                            IPointerExitHandler,
                            IPointerUpHandler,
                            IBeginDragHandler,
                            IDragHandler,
                            IEndDragHandler
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void VoidDelegateDrag(GameObject go, PointerEventData eventData);
    public VoidDelegate OnClick { get; set; }
    public VoidDelegate OnDown { get; set; }
    public VoidDelegate OnEnter { get; set; }
    public VoidDelegate OnLongPress { get; set; }
    public VoidDelegate OnExit { get; set; }
    public VoidDelegate OnUp { get; set; }
    public VoidDelegateDrag OnDragStart { get; set; }
    public VoidDelegateDrag OnDraging { get; set; }
    public VoidDelegateDrag OnDragEnd { get; set; }
 
    private string mAudioType;
    //是否处于按下状态 与长按配合使用
    bool isDown = false;
    float time = 0;
 
    public void OnPointerClick(PointerEventData eventData)
    {
        if (OnClick != null)
        {
            if (!GetComponent<Button>() || GetComponent<Button>().interactable)
            {
                OnClick(gameObject);
            }
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        OnDown?.Invoke(gameObject);
        time = Time.realtimeSinceStartup;
        isDown = true;
    }
    public void OnPointerEnter(PointerEventData eventData) { OnEnter?.Invoke(gameObject); }
    public void OnPointerExit(PointerEventData eventData) { OnExit?.Invoke(gameObject); isDown = false; }
    public void OnPointerUp(PointerEventData eventData) { OnUp?.Invoke(gameObject); isDown = false; time = Time.realtimeSinceStartup; }
    public void OnBeginDrag(PointerEventData eventData) { OnDragStart?.Invoke(gameObject, eventData); }
    public void OnDrag(PointerEventData eventData) { OnDraging?.Invoke(gameObject, eventData); }
    public void OnEndDrag(PointerEventData eventData) { OnDragEnd?.Invoke(gameObject, eventData); }
 
    public static UGUIEventListenerContainDrag Get(GameObject go, string clickAudio = null)
    {
        UGUIEventListenerContainDrag listener = go.GetComponent<UGUIEventListenerContainDrag>();
        if (listener == null) listener = go.AddComponent<UGUIEventListenerContainDrag>();
        listener.mAudioType = clickAudio;
        return listener;
    }
 
    void Update()
    {
        //长按
        if (OnLongPress != null && isDown && Time.realtimeSinceStartup - time > 0.8f)
        {
            OnLongPress(gameObject);
            isDown = false;
        }
    }
}