Intermediate Unity 3D for iOS: Part 3/3

This is a tutorial by Joshua Newnham, the founder of We Make Play, an independent studio crafting creative digital play for emerging platforms. Welcome back to our Intermediate Unity 3D for iOS tutorial series! In the first part of the series, you learned how to use the Unity interface to lay out a game scene, […] By .

Leave a rating/review
Save for later
Share

Learn how to make a simple 3D game with Unity!

Learn how to make a simple 3D game with Unity!

This is a tutorial by Joshua Newnham, the founder of We Make Play, an independent studio crafting creative digital play for emerging platforms.

Welcome back to our Intermediate Unity 3D for iOS tutorial series!

In the first part of the series, you learned how to use the Unity interface to lay out a game scene, and set up physics collisions.

In the second part of the series, you learned how to script with Unity, and created most of the game logic, including shooting and scoring baskets!

In this part and final of the tutorial, you’ll be working to build a menu system for the user and interacting with it via your GameController. Let’s get started!

Testing on your Device

So far, you have been testing with the built-in Unity simulator. But now that you have your project in a functional state it’s about time to run this on an actual device for testing!

To do this, open the Build dialog via File -> Build Settings. You’ll then be presented with the following dialog:

First off, ensure you have the right platform selected (iOS should have the Unity icon next to it as shown above, if not then highlight the iOS and click on the ‘Switch Platform’).

Select “Player Settings” to bring up the build setting in the inspector panel.

Unity3D build properties

There is a multitude of settings to select from, but (for now) you really only need to concern yourself with ensuring that the game is oriented correctly.

In the ‘Resolution and Presentation’ pane select Landscape-Left from the dropbox for the devices orientation and leave the rest as their defaults and move onto the next pane ‘Other Settings’.

In this section you’ll need to enter in your developer Bundle Identifier (this has the equivalent when developing in XCode) and leave the rest with their default values.

The Moment of Truth

Once you’ve set the appropriate values, go back to the Build Settings dialog and select Build.

Unity will prompt you for a destination for your project. Once you’ve selected the destination, Unity will launch XCode with your project ready to be built and run on your device.

Note: Don’t try running your game in a simulator as the Unity libraries are only for iOS devices. Running Unity projects on your device has all the usual requirements as far as certificates, App IDs and provisioning profiles go. Check out this thread on the Unity Answers site for more details.

The game running on an iPhone device!

Once you finish playing around come back and you’ll continue with building a simple menu for your game.

Keeping Score

First download the resources for this tutorial, which include some support classes that handle the persisting and retrieving of scores. Extract the zip and drag the .cs files into your Scripts folder in Unity.

The implementation of this is out of scope for this tutorial but as you will be using it you will run through a quick testable example of how it is used.

Start by creating a new empty GameObject on your scene and attach the LeaderboardController (included with this tutorials packages) script to it.

Create a new Script named LeaderboardControllerTest and attach it to a newly created GameObject. You will perform a simple test where you store a couple of scores and then retrieve them back.

To do this you need reference to the LeaderboardController so start off by adding a public LeaderboardController variable to your LeaderBoardControllerTest class, as shown below:

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

public class LeaderboardControllerTest : MonoBehaviour {
	public LeaderboardController leaderboard; 
		
	void Start () {	
	}
	
	void Update () {	
	}
}

Note: Take note of the using System.Collections.Generic; at the top of the class, this is asking that the classes belonging to the System.Collections.Generic package are included so you can reference the Generic specific collections. Explanation of Generic can be found here.

Use the AddPlayersScore method of the LeaderboardController to add the player score (who would have thought):

	
	void Start () {
		leaderboard.AddPlayersScore( 100 ); 
		leaderboard.AddPlayersScore( 200 ); 				
	}

This will persist the score to disk that you can retrieve even after you have closed the application. To retrieve you need to register for the LeaderboardControllers OnScoresLoaded event, along with the implemented handler method and finally request for the scores, as shown below.

By the way – the reason for the asynchronous call is to allow you to extend the LeaderboardController to handle a remote leaderboard later if you want.

		
	void Start () {
		leaderboard.OnScoresLoaded += Handle_OnScoresLoaded; 
 
		leaderboard.AddPlayersScore( 100 ); 
		leaderboard.AddPlayersScore( 200 );
 
		leaderboard.FetchScores();
	}
 
	public void Handle_OnScoresLoaded( List<ScoreData> scores ){		
		foreach( ScoreData score in scores ){
			Debug.Log ( score.points );
		}
	}

The parameter List is a list of ScoreData objects, the ScoreData is a simple data object that encapsulates the details of the score (a record).

The Handle_OnScoresLoaded method will iterate through all the scores and output their points, just what you need.

That’s it! Now test it out by doing the following:

  • Create a new GameObject, name it LeaderboardController, and attach the LeaderboardController script to it.
  • Select the LeaderboardControllerTest object, and attach the LeaderboardController object to the leaderboard property.
  • Click Play, and you should see the scores log out to the console!

Creating a simple menu

Now for something new and exciting – you’re going to learn how to create a menu for the game!

Here’s a screenshot of what you’re aiming to build:

There are three paths you can choose when implementing a user interface in Unity. Each has its advantages and disadvantages. The following section explains each in detail.

1) GUI

Unity provides a set of pre-defined user interface controls that can be implemented using the GUI Component via the MonoBehaviour hook method OnGUI. Unity also supports the ability to customize the appearance of the menu using Skins.

For scenes that aren’t performance-critical, this is an ideal solution as it provides the richest set of premade controls. However, as it has performance concerns, it should not be used during game play!

2) GUITexture and GUIText

Two components Unity provides are the GUITexture and GUIText. These components allow you to present flat (2D) images and text on the screen. You can easily extend these to create your user interface with a reduced hit on performance compared to using the GUI Component controls.

3) 3D Planes / Texture Altas

If you’re creating a heads up display (HUD i.e. a menu shown during game-play) then this is the preferred option; even though it requires the most effort! :] But once you have built the supporting classes for your heads up display, you can port them to every new project.

3D planes refers to implementing the HUD using a combination of flat 3D planes associated with a texture atlas, a texture atlas being a collection of many discrete images that has been saved as one large image. It’s similar in concept to a sprite sheet for all you Cocos2D users! :]

Since the Material (which references the texture) is shared across all of your HUD elements, usually only one call is required to render the HUD to the screen. In most cases, you would use a dedicated Camera for the HUD as its likely you’ll be rendering them in orthographic projection rather than perspective (which is a mode of the camera).

The option you will use in this tutorial is #1 – Unity’s GUI. Despite the above recommendations to avoid using it, it does have a host of pre-built controls which will keep this tutorial manageable! :]

You will start by creating the Skins for the main menu. Then you’ll work through the code which renders the main menu, and finally tie it all together by linking it with the GameController.

Sound good? Time to get started with skinning! :]