Last active
November 3, 2021 06:13
-
-
Save alexmelyon/cbe5005cf544bfba199ab22a16b88913 to your computer and use it in GitHub Desktop.
Send mouse/finger unity events
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
using UnityEngine; | |
using UnityEngine.Events; | |
public class MouseFingerEvent | |
{ | |
public Vector2 position; | |
public int index; | |
} | |
public class MouseFinger : MonoBehaviour | |
{ | |
public UnityEvent<MouseFingerEvent> OnInputBegin; | |
public UnityEvent<MouseFingerEvent> OnInputMove; | |
public UnityEvent<MouseFingerEvent> OnInputEnd; | |
void Update() | |
{ | |
handleMouse(0); | |
handleMouse(1); | |
handleMouse(2); | |
handleTouches(); | |
} | |
private void handleTouches() | |
{ | |
for (int finger = 0; finger < Input.touchCount; finger++) | |
{ | |
var touch = Input.touches[finger]; | |
var ev = new MouseFingerEvent { | |
position = touch.position, | |
index = finger | |
}; | |
if (touch.phase == TouchPhase.Began) | |
{ | |
OnInputBegin.Invoke(ev) ; | |
} | |
if (touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) | |
{ | |
OnInputMove.Invoke(ev); | |
} | |
if (touch.phase == TouchPhase.Ended) | |
{ | |
OnInputEnd.Invoke(ev); | |
} | |
} | |
} | |
private void handleMouse(int button) | |
{ | |
var ev = new MouseFingerEvent | |
{ | |
position = Input.mousePosition, | |
index = button | |
}; | |
if (Input.GetMouseButtonDown(button)) | |
{ | |
OnInputBegin.Invoke(ev); | |
} | |
if(Input.GetMouseButton(button)) | |
{ | |
OnInputMove.Invoke(ev); | |
} | |
if (Input.GetMouseButtonUp(button)) | |
{ | |
OnInputEnd.Invoke(ev); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment