Skip to content

Commit

Permalink
- add TouchManager and Update version
Browse files Browse the repository at this point in the history
  • Loading branch information
DucNV2000 committed Jul 5, 2024
1 parent 8b2bcc7 commit e25eaac
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 4 deletions.
126 changes: 126 additions & 0 deletions Core/TouchInputManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using VirtueSky.Inspector;

namespace VirtueSky.TouchInput
{
[HideMonoScript]
public class TouchInputManager : MonoBehaviour
{
public static event Action<Touch> InputEventTouchBegin;
public static event Action<Touch> InputEventTouchMove;
public static event Action<Touch> InputEventTouchStationary;
public static event Action<Touch> InputEventTouchEnd;
public static event Action<Touch> InputEventTouchCancel;

public static event Action<Vector3> InputEventMouseDown;
public static event Action<Vector3> InputEventMouseUpdate;
public static event Action<Vector3> InputEventMouseUp;

[SerializeField, Tooltip("Use mouse in editor")]
private bool useMouse = false;

[SerializeField, Tooltip("Disable when touching UI")]
private bool ignoreUI = true;

[ShowIf(nameof(IsPlaying)), SerializeField]
private Vector3 touchPosition;

private bool IsPlaying => Application.isPlaying;
private bool _mouseDown;
private bool _mouseUpdate;

private void Update()
{
if (ignoreUI)
{
if (Input.touchCount > 0 && EventSystem.current.currentSelectedGameObject == null)
{
HandleTouch();
}
}
else
{
if (Input.touchCount > 0)
{
HandleTouch();
}
}
#if UNITY_EDITOR
if (useMouse)
{
if (ignoreUI)
{
if (EventSystem.current.currentSelectedGameObject == null)
{
HandleMouse();
}
}
else
{
HandleMouse();
}
}

#endif
}

void HandleTouch()
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
InputEventTouchBegin?.Invoke(touch);

break;
case TouchPhase.Moved:
InputEventTouchMove?.Invoke(touch);

break;
case TouchPhase.Stationary:
InputEventTouchStationary?.Invoke(touch);

break;
case TouchPhase.Ended:
InputEventTouchEnd?.Invoke(touch);

break;
case TouchPhase.Canceled:
InputEventTouchCancel?.Invoke(touch);

break;
}

touchPosition = touch.position;
}

void HandleMouse()
{
if (Input.GetMouseButtonDown(0))
{
if (!_mouseDown)
{
_mouseDown = true;
_mouseUpdate = true;
InputEventMouseDown?.Invoke(Input.mousePosition);
touchPosition = Input.mousePosition;
}
}
else if (Input.GetMouseButtonUp(0))
{
_mouseDown = false;
_mouseUpdate = false;
InputEventMouseUp?.Invoke(Input.mousePosition);
touchPosition = Input.mousePosition;
}

if (_mouseDown && _mouseUpdate)
{
InputEventMouseUpdate?.Invoke(Input.mousePosition);
touchPosition = Input.mousePosition;
}
}
}
}
3 changes: 3 additions & 0 deletions Core/TouchInputManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Core/UnityCommon.Core.asmdef
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"name": "UnityCommon.Core"
"name": "UnityCommon.Core",
"references": [
"GUID:324caed91501a9c47a04ebfd87b68794"
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@

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

for version `1.0.1`
for version `1.0.2`
```csharp
"com.wolf-package.extensions":"https://github.com/wolf-package/extensions-unity.git#1.0.1",
"com.wolf-package.extensions":"https://github.com/wolf-package/extensions-unity.git#1.0.2",
```
## Use
- [Document](https://github.com/wolf-package/extensions-unity/wiki)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.wolf-package.extensions",
"displayName": "UnityCommon-Extensions",
"description": "Extensions for Unity",
"version": "1.0.1",
"version": "1.0.2",
"unity": "2021.3",
"category": "virtuesky",
"license": "MIT",
Expand Down

0 comments on commit e25eaac

Please sign in to comment.