1
+ using System ;
2
+ using UnityEngine ;
3
+ using UnityEngine . EventSystems ;
4
+ using VirtueSky . Inspector ;
5
+
6
+ namespace VirtueSky . TouchInput
7
+ {
8
+ [ HideMonoScript ]
9
+ public class TouchInputManager : MonoBehaviour
10
+ {
11
+ public static event Action < Touch > InputEventTouchBegin ;
12
+ public static event Action < Touch > InputEventTouchMove ;
13
+ public static event Action < Touch > InputEventTouchStationary ;
14
+ public static event Action < Touch > InputEventTouchEnd ;
15
+ public static event Action < Touch > InputEventTouchCancel ;
16
+
17
+ public static event Action < Vector3 > InputEventMouseDown ;
18
+ public static event Action < Vector3 > InputEventMouseUpdate ;
19
+ public static event Action < Vector3 > InputEventMouseUp ;
20
+
21
+ [ SerializeField , Tooltip ( "Use mouse in editor" ) ]
22
+ private bool useMouse = false ;
23
+
24
+ [ SerializeField , Tooltip ( "Disable when touching UI" ) ]
25
+ private bool ignoreUI = true ;
26
+
27
+ [ ShowIf ( nameof ( IsPlaying ) ) , SerializeField ]
28
+ private Vector3 touchPosition ;
29
+
30
+ private bool IsPlaying => Application . isPlaying ;
31
+ private bool _mouseDown ;
32
+ private bool _mouseUpdate ;
33
+
34
+ private void Update ( )
35
+ {
36
+ if ( ignoreUI )
37
+ {
38
+ if ( Input . touchCount > 0 && EventSystem . current . currentSelectedGameObject == null )
39
+ {
40
+ HandleTouch ( ) ;
41
+ }
42
+ }
43
+ else
44
+ {
45
+ if ( Input . touchCount > 0 )
46
+ {
47
+ HandleTouch ( ) ;
48
+ }
49
+ }
50
+ #if UNITY_EDITOR
51
+ if ( useMouse )
52
+ {
53
+ if ( ignoreUI )
54
+ {
55
+ if ( EventSystem . current . currentSelectedGameObject == null )
56
+ {
57
+ HandleMouse ( ) ;
58
+ }
59
+ }
60
+ else
61
+ {
62
+ HandleMouse ( ) ;
63
+ }
64
+ }
65
+
66
+ #endif
67
+ }
68
+
69
+ void HandleTouch ( )
70
+ {
71
+ Touch touch = Input . GetTouch ( 0 ) ;
72
+ switch ( touch . phase )
73
+ {
74
+ case TouchPhase . Began :
75
+ InputEventTouchBegin ? . Invoke ( touch ) ;
76
+
77
+ break ;
78
+ case TouchPhase . Moved :
79
+ InputEventTouchMove ? . Invoke ( touch ) ;
80
+
81
+ break ;
82
+ case TouchPhase . Stationary :
83
+ InputEventTouchStationary ? . Invoke ( touch ) ;
84
+
85
+ break ;
86
+ case TouchPhase . Ended :
87
+ InputEventTouchEnd ? . Invoke ( touch ) ;
88
+
89
+ break ;
90
+ case TouchPhase . Canceled :
91
+ InputEventTouchCancel ? . Invoke ( touch ) ;
92
+
93
+ break ;
94
+ }
95
+
96
+ touchPosition = touch . position ;
97
+ }
98
+
99
+ void HandleMouse ( )
100
+ {
101
+ if ( Input . GetMouseButtonDown ( 0 ) )
102
+ {
103
+ if ( ! _mouseDown )
104
+ {
105
+ _mouseDown = true ;
106
+ _mouseUpdate = true ;
107
+ InputEventMouseDown ? . Invoke ( Input . mousePosition ) ;
108
+ touchPosition = Input . mousePosition ;
109
+ }
110
+ }
111
+ else if ( Input . GetMouseButtonUp ( 0 ) )
112
+ {
113
+ _mouseDown = false ;
114
+ _mouseUpdate = false ;
115
+ InputEventMouseUp ? . Invoke ( Input . mousePosition ) ;
116
+ touchPosition = Input . mousePosition ;
117
+ }
118
+
119
+ if ( _mouseDown && _mouseUpdate )
120
+ {
121
+ InputEventMouseUpdate ? . Invoke ( Input . mousePosition ) ;
122
+ touchPosition = Input . mousePosition ;
123
+ }
124
+ }
125
+ }
126
+ }
0 commit comments