少年修仙传客户端代码仓库
hch
2025-06-12 204ef05a831c9484e2abc561d27ecbff7c797453
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
//--------------------------------------------------------
//    [Author]:           第二世界
//    [  Date ]:           Monday, July 31, 2017
//--------------------------------------------------------
 
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
 
namespace vnxbqy.UI {
 
    /// <summary>
    /// 延迟触发点击事件的按钮,用于长按事件
    /// </summary>
    public class DelayButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler {
 
        [SerializeField]
        float m_Delay = 0.5f;
        public float delay { get { return m_Delay; } }
 
        [SerializeField]
        UIEvent m_OnClick;
        public UIEvent onClick { get { return m_OnClick; } }
 
        float timer = 0f;
        bool down = false;
 
        public void OnPointerDown(PointerEventData eventData) {
            timer = 0f;
            down = true;
        }
 
        public void OnPointerUp(PointerEventData eventData) {
            down = false;
        }
 
        private void OnEnable() {
            timer = 0f;
            down = false;
        }
 
        private void LateUpdate() {
            if(down && timer < delay) {
                timer += Time.deltaTime;
                if(timer > delay) {
                    if(onClick != null) {
                        onClick.Invoke();
                    }
                }
            }
        }
 
    }
 
}