Last active
July 19, 2023 17:42
-
-
Save sdabet/3bda94676a4674e6e4a0 to your computer and use it in GitHub Desktop.
Simulating touch events from mouse events in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update () { | |
// Handle native touch events | |
foreach (Touch touch in Input.touches) { | |
HandleTouch(touch.fingerId, Camera.main.ScreenToWorldPoint(touch.position), touch.phase); | |
} | |
// Simulate touch events from mouse events | |
if (Input.touchCount == 0) { | |
if (Input.GetMouseButtonDown(0) ) { | |
HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Began); | |
} | |
if (Input.GetMouseButton(0) ) { | |
HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Moved); | |
} | |
if (Input.GetMouseButtonUp(0) ) { | |
HandleTouch(10, Camera.main.ScreenToWorldPoint(Input.mousePosition), TouchPhase.Ended); | |
} | |
} | |
} | |
private void HandleTouch(int touchFingerId, Vector3 touchPosition, TouchPhase touchPhase) { | |
switch (touchPhase) { | |
case TouchPhase.Began: | |
// TODO | |
break; | |
case TouchPhase.Moved: | |
// TODO | |
break; | |
case TouchPhase.Ended: | |
// TODO | |
break; | |
} | |
} |
thank you good sir.
perfect snippet.
Thank you very much
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thnx!