hch
65 分钟以前 5c61793c3404cd912c8357eef5fef70bae5bd3d8
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
 
    public class LongPressButton : ButtonEx
    {
        public class ButtonPressEvent : UnityEvent { }
 
        [FormerlySerializedAs("onPress")]
        [SerializeField]
        private ButtonPressEvent m_OnPress = new ButtonPressEvent();
        public ButtonPressEvent onPress
        {
            get { return m_OnPress; }
            set { m_OnPress = value; }
        }
 
        [SerializeField]
        float m_LongPressCheckTime = 0.5f;
        public float longPressCheckTime
        {
            get { return m_LongPressCheckTime; }
            set { m_LongPressCheckTime = value; }
        }
        [SerializeField]
        float m_LongPressIntervalTime = 0.1f;
        public float longPressIntervalTime
        {
            get { return m_LongPressIntervalTime; }
            set { m_LongPressIntervalTime = value; }
        }
 
        float m_PressTime = 0.0f;
        float m_PressIntervalTime = 0.0f;
        bool m_LongPress = false;
        bool m_IsButtonDown = false;
 
        public override void OnPointerClick(PointerEventData eventData)
        {
            if (m_LongPress)
            {
                m_LongPress = false;
                return;
            }
            base.OnPointerClick(eventData);
        }
 
        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);
            m_PressTime = 0.0f;
            m_PressIntervalTime = 0.0f;
            m_IsButtonDown = true;
        }
 
        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);
            m_IsButtonDown = false;
        }
 
        public override void OnPointerExit(PointerEventData eventData)
        {
            base.OnPointerExit(eventData);
            m_IsButtonDown = false;
            m_LongPress = false;
        }
 
        private void Update()
        {
            if (m_IsButtonDown)
            {
                m_PressTime += Time.deltaTime;
                if (m_PressTime < m_LongPressCheckTime)
                {
                    return;
                }
                if (m_PressIntervalTime >= m_LongPressIntervalTime)
                {
                    onPress.Invoke();
                    m_PressIntervalTime = 0.0f;
                }
                m_PressIntervalTime += Time.deltaTime;
                m_LongPress = true;
            }
        }
 
        protected override void OnEnable()
        {
            base.OnEnable();
            m_LongPress = false;
            m_IsButtonDown = false;
        }
    }