Basic iOs Input Tutorial

From Unify Community Wiki
Jump to: navigation, search


Basic iOS Input Tutorial


How to get basic input from an iOS device.

Assumes you have your app running on an iOS device (e.g., iPad), either using Unity Remote or deploying via XCode.

In this tutorial:

  • Tap - a tap on the iPad screen
  • Swipe - dragging your finger across the iPad screen (i.e., extended touch event)

Tap Input

The following .js code should work for "tap" inputs. Put it in a script attached to one of your objects.

function Update() {	
    if(Input.GetButton("Fire1")) {
function TapFunction() {
    //Respond to tap event
    //Note that "TapFunction" can be called anything you like.
    //You could also omit TapFunction(), and just respond inside of Update(). But putting the response in a function is usually the way to go.


If it doesn't work, check your Input Manager settings (find it in the Project Settings menu). Ensure you have an axes called "Fire1" (a default Unity install will have it), and check these settings:

  • Type - should be "Key or Mouse Button"
  • Joy Num - should be "Get Motion from all Joysticks"

You can also use alternate inputs to ensure your app is responding correctly:

  • By default, Unity also binds Fire1 to the left mouse button, and the left Control key on the keyboard. So try those and see if your app responds.
  • You can do this to add additional options:
if(Input.GetButton("Fire1") || Input.GetKeyDown("z")) //true on: iPad tap, left mouse button, left Control, and the "z" key on the keyboard (default Input Manager settings)

Swipe Input

The following .cs code should work for "swipe" inputs:

float swipeSpeed = 0.05F;
float inputX;
float inputY;
void FixedUpdate() {
  if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
    touchDeltaPosition = Input.GetTouch(0).deltaPosition;						 
    inputX += touchDeltaPosition.x * swipeSpeed;
    inputY += touchDeltaPosition.y * swipeSpeed;
    Debug.Log("X, Y: " + touchDeltaPosition.x	+ ", " + touchDeltaPosition.y);

Now you can use inputX and inputY to do whatever you need!

Personal tools