Skip to content

Commit 857e304

Browse files
committed
added a very basic mouse events sample
1 parent 4c0bab2 commit 857e304

File tree

5 files changed

+504
-0
lines changed

5 files changed

+504
-0
lines changed

Assets/Samples/MouseEvents.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.InputSystem;
4+
using UnityEngine.InputSystem.EnhancedTouch;
5+
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
6+
7+
public class OnMouseEventsTest : MonoBehaviour
8+
{
9+
private Vector3 screenPoint;
10+
private Vector3 offset;
11+
12+
private void Start()
13+
{
14+
EnhancedTouchSupport.Enable();
15+
}
16+
17+
void OnMouseDown()
18+
{
19+
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
20+
float x;
21+
float y;
22+
if (Pointer.current != null)
23+
{
24+
x = Pointer.current.position.x.value;
25+
y = Pointer.current.position.y.value;
26+
}
27+
else // Fallback to InputManager if InputSystem is not available
28+
{
29+
x = Input.mousePosition.x;
30+
y = Input.mousePosition.y;
31+
}
32+
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(x, y, screenPoint.z));
33+
}
34+
35+
void OnMouseDrag()
36+
{
37+
Vector3 curScreenPoint;
38+
if (Pointer.current != null)
39+
curScreenPoint = new Vector3(Pointer.current.position.x.value, Pointer.current.position.y.value, screenPoint.z);
40+
else // Fallback to InputManager if InputSystem is not available
41+
curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
42+
43+
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
44+
transform.position = curPosition;
45+
}
46+
}

Assets/Samples/MouseEvents/OnMouseEventsTest.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)