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