Skip to content

Commit 6429e90

Browse files
committed
refactored OnMouseEventSample
1 parent 77c89af commit 6429e90

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 OnMouseEventSample : MonoBehaviour
8+
{
9+
private Vector3 screenPoint;
10+
private Vector3 offset;
11+
12+
void OnMouseDown()
13+
{
14+
if (Pointer.current == null)
15+
return;
16+
17+
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
18+
float x = Pointer.current.position.x.value;
19+
float y = Pointer.current.position.y.value;
20+
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(x, y, screenPoint.z));
21+
}
22+
23+
void OnMouseDrag()
24+
{
25+
if (Pointer.current == null)
26+
return;
27+
28+
Vector3 curScreenPoint = new Vector3(Pointer.current.position.x.value, Pointer.current.position.y.value, screenPoint.z);
29+
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
30+
transform.position = curPosition;
31+
}
32+
}
File renamed without changes.

Assets/Samples/MouseEvents/OnMouseEventsTest.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)