MouseToTouch

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "</csharp>" to "</syntaxhighlight>")
(improve code.)
Line 1: Line 1:
[[Category: C Sharp]]
+
[[Category:Code Examples]]
[[Category: Scripts]]
+
[[Category:Input]]
Author: [[User:Kiyaku|Kiyaku]]
+
  
 
== Description ==
 
== Description ==
Sometimes, when i am working on an iPhone Application, the Client wants to try the game without using an iPhone.
+
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.
So i wrote a little script that converts some Mouse Functions into iPhoneInput function.  
+
You just have to add a few lines but can keep your original gameplay code.  
+
  
Right now it can recognize touchCount 0 or 1, positions and returns "iPhoneTouchPhase Began, Moved, Ended".
 
  
Feel free to extend it!
+
== MouseToTouch.cs ==
 
+
=== Usage ===
+
 
+
Create a file named "iPhoneToMouse.cs" and add following code to it:
+
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
 
using UnityEngine;
 
using UnityEngine;
using System.Collections;
 
  
public class iPhoneToMouse
+
public class MouseToTouch
 
{
 
{
public int touchCount
+
    public enum TouchPhase { Moved, Ended, Began }
{
+
get
+
{
+
if(Input.GetMouseButton(0) || Input.GetMouseButton(1) ||
+
  Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) )
+
return 1;
+
else
+
return 0;
+
}
+
}
+
+
+
public pos GetTouch(int ID)
+
{
+
pos tempPos = new pos();
+
+
tempPos.position = Input.mousePosition;
+
+
if(Input.GetMouseButtonDown(ID))
+
tempPos.phase = iPhoneTouchPhase.Began;
+
else if(Input.GetMouseButton(ID))
+
tempPos.phase = iPhoneTouchPhase.Moved;
+
+
if(Input.GetMouseButtonUp(ID))
+
tempPos.phase = iPhoneTouchPhase.Ended;
+
  
return tempPos;
+
    public struct TouchData
}
+
    {
+
        public Vector2 Position;
 +
        public TouchPhase Phase;
 +
    }
  
public struct pos
+
    public int TouchCount
{
+
    {
public Vector2 position;
+
        get
public iPhoneTouchPhase phase;
+
        {
}
+
            if (Input.GetMouseButton(0) || Input.GetMouseButton(1) ||
}
+
              Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1))
 +
            {
 +
                return 1;
 +
            }
 +
            else
 +
            {
 +
                return 0;
 +
            }
  
 +
        }
 +
    }
  
public enum iPhoneTouchPhase
+
    public TouchData GetTouch(int id)
{
+
    {
Moved,
+
        var touch = new TouchData();
Ended,
+
 
Began
+
        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 ==
  
Add following lines to your scripts which include iPhoneTouch commands:
+
Create an instance of the script to use it.
  
 
<syntaxhighlight lang="csharp">
 
<syntaxhighlight lang="csharp">
Line 80: Line 75:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
That's it!
 

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

Variants
Actions
Navigation
Extras
Toolbox