MouseToTouch
From Unify Community Wiki
(Difference between revisions)
Isaiah Kelly (Talk | contribs) (improve code.) |
Isaiah Kelly (Talk | contribs) m (Isaiah Kelly moved page IPhoneToMouse to MouseToTouch: rename page to match new script name) |
Latest revision as of 00:17, 19 October 2020
[edit] Description
A little script that converts mouse input to touch input. Right now it can recognize touchCount 0 or 1, positions and returns TouchPhase state began, moved or ended.
[edit] MouseToTouch.cs
using UnityEngine; public class MouseToTouch { public enum TouchPhase { Moved, Ended, Began } public struct TouchData { public Vector2 Position; public TouchPhase Phase; } public int TouchCount { get { if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { return 1; } else { return 0; } } } public TouchData GetTouch(int id) { var touch = new TouchData(); touch.Position = Input.mousePosition; if (Input.GetMouseButtonDown(id)) { touch.Phase = TouchPhase.Began; } else if (Input.GetMouseButton(id)) { touch.Phase = TouchPhase.Moved; } if (Input.GetMouseButtonUp(id)) { touch.Phase = TouchPhase.Ended; } return touch; } }
[edit] Usage
Create an instance of the script to use it.
private iPhoneToMouse iPhoneInput; void Start() { iPhoneInput = new iPhoneToMouse(); }