Unity to iPhone – build games for Apple’s mobile device

 by • Reading Time: 7 minutes

Introduction

One of the features in Unity is the build system that allows deploying applications on mobile devices such as iPhone. In this step-by-step guide I’ll show how to build and run games on Apple devices. Please note that methodology described here assumes that you already possess the Apple Developer account. Furthermore, you’ll need access to a Mac machine with the latest version of XCode installed.

Table of contents

  1. Unity mobile buttons sprites for iPhone touchscreen
  2. Controls integration in Unity for iPhone
  3. From Unity to iPhone – build and play the game on Apple’s mobile device

1. Unity mobile buttons sprites for iPhone touchscreen

For the purpose of this tutorial I’m going to use all elements developed in the recent articles. I would like to encourage you to read them first before you continue. The articles are on top-down movement and attack, fighting mechanics and tilemaps system.

Let’s start by creating four directional pad (d-pad) buttons for moving our character. Then I’ll add two more “action” buttons for executing attack. By doing so we will have all GUI elements to control our character with a touch of a screen! That is to say, it’s a simple spritesheet I’ve created in Aseprite. I imported it to Unity using the same methods as in previous articles. However, the difference here is that I sliced the spritesheet manually.

Importing and manually slicing the buttons in Unity

After that, you have all button sprites you need. I placed directional arrows on the left and action buttons on the right. By doing so the positioning resembles the classic NES game controller layout. Let’s now look into how to make our buttons respond to our screen touches!

2. Controls integration in Unity for iPhone

In this section we are going to implement the control functionality for our character. We will assign custom functions to events that are triggered whenever user presses or releases buttons we have defined.

Let’s move! D-Pad controls

The buttons are ready but now we have to write few ‘trigger functions’. They will be used every time when user clicks and holds our buttons. To that end I’m going to use ‘EventTrigger’ component. The reason is that native button construct in Unity causes some problems when events are triggered on iPhone.

  • open ‘PlayerMovement’ script that is currently attached to ‘Player’
  • after that move the entire movement logic out of ‘Update()’ to a separate function. I’ve intentionally removed output coming from Input.GetAxisRaw() functions. I won’t need them to collect input from the mobile’s touchscreen.
private void NotifyMovementAnimator()
{
    animator.SetFloat("Horizontal", movement.x);
    animator.SetFloat("Vertical", movement.y);
    animator.SetFloat("Speed", movement.sqrMagnitude);

    if (movement.x == 1 || movement.x == -1 || movement.y == 1 || movement.y == -1)
    {
        animator.SetFloat("Last_Horizontal", movement.x);
        animator.SetFloat("Last_Vertical", movement.y);
    }
}
  • create a function to collect input coming from buttons pressed by user. Note that I’m also assigning ‘zero’ vector to ‘movement’ vector in case user is not holding any of them.
private void SetMovementVector()
{
    if (isPressUp) movement.y = 1;
    if (isPressDown) movement.y = -1;
    if (isPressLeft) movement.x = -1;
    if (isPressRight) movement.x = 1;
    if (!isPressUp && !isPressDown && 
        !isPressLeft && !isPressRight) 
          movement = Vector2.zero;
}
  • add two directional functions: OnPress<direction>() and OnRelease<direction>(). Set ‘boolean’ values accordingly to control the button behaviour. Create this pair of functions for each directional button.
public void onPressUp()
{
    isPressUp = true;
}

public void onReleaseUp()
{
    isPressUp = false;
}
  • execute both functions in the main Update() loop
void Update()
{
    SetMovementVector();
    NotifyMovementAnimator();
}

To sum up the functionality, assign each of the ‘control’ functions to appropriate event triggers.

  • click on the directional button and in the ‘Inspector’ panel add ‘EventTrigger’ component
  • after that create two trigger events, namely ‘Pointer Down’ and ‘Pointer Up’. The former is for when the user presses and holds the button while the latter is when he releases it.
  • drag and drop the ‘Player’ game asset from project ‘Hierarchy’ panel. Select the appropriate functions from the list
Event Trigger Setup For Directional Buttons

How about some action! Action controls

Please note that in order for directional buttons to work you have to assign the correct functions to them. In short, this is done so that the character movement is natural. However, you may wish to play with these settings later on during times when you want to disorient the user. For example, swap the directional controls whenever character is under the influence of a confusion spell. That is to say, we also need to have a possibility to ‘act’ upon events occurring inside our game. To that end we are going to use our action buttons.

If you have followed the tutorial on fighting mechanics, then the animator is already correctly set. The only thing that we are going to do is to write the function that triggers the attack.

  • add a function for executing the attack
public void onPressAttack()
{
    animator.SetBool("isAttack", true);
}
  • add ‘EventTrigger’ component to action buttons
  • create ‘Pointer Click’ event and assign the correct function to it
Setting up the event trigger for action buttons

Now we have all the necessary controls in place it’s time to deploy our mobile game to iPhone!

3. From Unity to iPhone – build and play the game on Apple’s mobile device

There are few steps before we can begin deployment of our Unity project to iPhone device. Firstly, we have to create necessary files that are required during building procedure. Secondly, we need to build the Unity project using the correct platform settings. Thirdly, we need to load it in XCode and set the ‘Provisioning Profile’ obtained from ‘Apple’s Developer’ account. Let’s do this step by step!

Register new developer certificate

Log into your Apple Developer account. Let’s start by creating a developer certificate for our Mac. Under the ‘Certificates’ click on ‘+’ sign and select ‘iOS App Development’. You will be required to provide a CSR (Certificate Signing Request) file. You can easily generate one in ‘Keychain Access’ application. Follow the official Apple guide on how to generate the CSR file and submit it. After that you can download the generated certificate to a safe location.

Add iPhone mobile device

Let’s now register the mobile device to which we want to deploy our game. Go to ‘Devices’ section and click on ‘+’ sign. You will have to provide the device ID called UDID. It’s a long unique identification number of your iPhone. If you are using the latest Mac OS you can check it in ‘Music’ application. Once you connect the device, repeatedly click on the name and icon in the top bar until you get it.

Finding iPhone application in 'Music' app

Create ‘application identifier’

Once you added the device it’s time to create ‘Application Identifier’. This is a name our application is going to work under. It is also required for creating a ‘Provisioning Profile’ during building process using XCode.

  • go to ‘Identifiers’ and click ‘+’ sign.
  • register it as ‘App ID’s’ and click ‘Continue’.
New Application Identifier in Apple Developer Program
  • provide a ‘Description’ and ‘Bundle ID’. As a good rule of thumb Apple recommends to use the inverted domain name of our company. I’m going to use ‘com.pavcreations.dungenon’.
Specifying game's bundle ID
  • click ‘Save’

Create application ‘provisioning profile’

We have registered the certificate, added the device and created an application identifier. As a result, we are now ready to create a provisioning profile. It is required for building applications using XCode. Click on ‘Profiles’, register a new ‘iOS App Development’ and ‘Continue’.

Creating a provisioning profile in Apple's Developer account

In the next panel select the App ID we’ve created in previous step. After that, select a certificate we have registered in the beginning. Provide mobile device details you added to your developer’s account earlier. In the last step enter your desired name for this provisioning profile and click ‘Generate’. After that you can download your provisioning profile. Mind the location of this file as we are going to need it soon!

Building Unity project

Building a Unity project is fairly simple and can be done in few clicks. Select ‘File -> Building Settings…’ and change your destination platform to iOS. You can leave all the default options and click on ‘Build’. Important note: you may have to click on ‘Switch Platform’ before you can build your project for iPhone!

Screenshot showing the building settings for deploying Unity Project to iPhone mobile device

Select a folder where your project is going to be build. Take note of the location path as we are going to need it in the next step.

Building and deploying the Unity game to iPhone mobile device

In this final step we are going to deploy our game to iPhone device!

  • connect your iPhone to Mac and start XCode
  • select the Unity project destination path in order to open it
  • after that choose your device from the list of available devices and click on project root
  • in ‘General’ tab enter the ‘Display Name’ and ‘Bundle Identifier’ we have defined earlier in ‘Apple Developer’ account
  • in the ‘Signing & Capabilities’ tab under ‘All’ specify the ‘Provisioning Profile’
 XCode settings for deploying Unity project in iPhone.

Do this by importing the certificate file in both fields. Congratulations! You have now deployed our great game to iPhone!

Conclusion

I’ve outlined all necessary steps to build and deploy Unity projects to iPhone. Firstly, I’ve created a number of HUD (head-up display) buttons in Aseprite. Secondly, I placed them in intuitive places to mimic the layout of a classic NES controller. Thirdly, I looked at the functionality of the buttons, triggers and their correct assignment. Lastly, the Unity project was built and later deployed to iPhone using XCode.