Skip to content

Commit e25eaac

Browse files
committed
- add TouchManager and Update version
1 parent 8b2bcc7 commit e25eaac

File tree

5 files changed

+136
-4
lines changed

5 files changed

+136
-4
lines changed

Core/TouchInputManager.cs

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
}

Core/TouchInputManager.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/UnityCommon.Core.asmdef

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"name": "UnityCommon.Core"
2+
"name": "UnityCommon.Core",
3+
"references": [
4+
"GUID:324caed91501a9c47a04ebfd87b68794"
5+
]
36
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323

2424
### Add the line below to `Packages/manifest.json`
2525

26-
for version `1.0.1`
26+
for version `1.0.2`
2727
```csharp
28-
"com.wolf-package.extensions":"https://github.com/wolf-package/extensions-unity.git#1.0.1",
28+
"com.wolf-package.extensions":"https://github.com/wolf-package/extensions-unity.git#1.0.2",
2929
```
3030
## Use
3131
- [Document](https://github.com/wolf-package/extensions-unity/wiki)

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "com.wolf-package.extensions",
33
"displayName": "UnityCommon-Extensions",
44
"description": "Extensions for Unity",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"unity": "2021.3",
77
"category": "virtuesky",
88
"license": "MIT",

0 commit comments

Comments
 (0)