How to make Buttons work on mobile?

Hello!!

I am working on a 2D Platformer for mobile. On the main menu screen I have two buttons. One is play and the other is quit. I am just wondering how would I make the buttons take a touch input once it is on the phone. I did switch the build to Andorid. is that all that is needed or is there something else? I am doing the project in c#.

Thanks!!!

You need to import the “CrossPlatformInput” package.

Go to menu → Assets → Import Package → CrossPlatformInput

It will add the standard assets → CrossPlatformInput folder into your project.

Drag the MobileSingleStickController.prefab in to your scene. It has a joystick and jump button. Remove the joystick and you can use the jump button as your play button.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;


public class gamePlay : MonoBehaviour {

    void Start() {
       
    }

    // Update is called once per frame
    void Update () {

        if (CrossPlatformInputManager.GetButtonDown("Jump")) {
             // do your code here
        }
	}
}

Notice the “using UnityStandardAssets.CrossPlatformInput;” is required

You can clone the button to make a quit version also.

You can also just use a canvas image. Create your canvas image then attach this script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class playButton : MonoBehaviour, IPointerClickHandler {

    public void OnPointerClick(PointerEventData eventData) {
        // OnClick code goes here ...
       playGame();
    }


	// Use this for initialization
	void Start () {

    }
	
	// Update is called once per frame
	void Update () {
		
	}
}

You don’t need to use anything special. Those UI buttons will work on Android.

attach StartGame method and QuitGame method to the respective buttons. It will work on Android.

in StartGame put

SceneManager.LoadScene(“Your Game scene Name”)

and in QuitGame simply put

Application.quit();

I would recommend you to use Unity’s remote 5 so that you can check everything on the go.