MouseToTouch
From Unify Community Wiki
(Difference between revisions)
m (Text replace - "</csharp>" to "</syntaxhighlight>") |
Isaiah Kelly (Talk | contribs) (improve code.) |
||
Line 1: | Line 1: | ||
− | [[Category: | + | [[Category:Code Examples]] |
− | [[Category: | + | [[Category:Input]] |
− | + | ||
== Description == | == 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. | |
− | + | ||
− | + | ||
− | |||
− | + | == MouseToTouch.cs == | |
− | + | ||
− | === | + | |
− | + | ||
− | + | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
using UnityEngine; | using UnityEngine; | ||
− | |||
− | public class | + | 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 | + | 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; | ||
+ | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | == Usage == | ||
− | + | Create an instance of the script to use it. | |
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
Line 80: | Line 75: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− |
Revision as of 00:12, 19 October 2020
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.
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; } }
Usage
Create an instance of the script to use it.
private iPhoneToMouse iPhoneInput; void Start() { iPhoneInput = new iPhoneToMouse(); }