MouseToTouch

From Unify Community Wiki
Jump to: navigation, search


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();
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox