Time for action – Identifying the type of iOS

Let's suppose that we have designed our application such that it is optimized for high definition (HD) on iPhone 4 and the iPad.

Tip

If the player runs this version of our application we want to tell them that the application would perform better if they used the standard definition (SD) version of the game.

We will do this by detecting the device and either starting the application immediately or showing them a notice screen and then starting the application.

Let's walk through the steps that would be necessary for this application functionality.

  1. Identify the type of device we are using.
  2. Based on conditional logic display a splash screen.
  3. Dismiss the splash screen.

Identify our device information

What we need to do is:

  1. Add some device detection to the Start() script that we're attaching to our Camera.
  2. What we're looking for are iOS generations that are older than the iPhone4 and the iPad. We can check for this by looking at the value of iPhoneSettings.generation.
  3. We choose iPhoneSettings.generation as opposed to iPhoneSettings.model because generation will return us an enumerated type and since all of the models are represented – this will be less error prone than trying to compare strings.
    void Start () {
      if ( ! ( ( iPhoneSettings.generation == iPhoneGeneration.iPhone4 ) ||
      ( iPhoneSettings.generation == iPhoneGeneration.iPad1Gen ) ) )
      {
        // this device isn't an iPad or an iPhone4 and therefore // doesn't support HD.
      }
    
    
    }
    

Conditionally show splash screen

Now that we know whether or not we have an HD "qualified" device, we have the ability to conditionally display a screen. In this example we have built out our SD display as a level all on its own. This was done so that we could compartmentalize this body of work by itself without it cluttering up the rest of the game.

  1. To load this SD purchase suggestion level we make a call to the Application.LoadLevel() method and provide it the name of the level that we want to load – in this case it will be our "SDAdvisory" level.
    Application.LoadLevel("SDAdvisory");

Unity will now load this level and display it on the screen. Since we've set its background to just be an image for displaying this information we need to provide a mechanism for this advisory to be dismissed.

Dismiss the splash screen

In our new scene we want to be able to acknowledge that we've seen the advisory that we should be using the SD version of the game, yet proceed to playing the game in high definition. If you aren't building universal binaries of your game (which you should!), you should check for the appropriateness of the version of the content the user is attempting to run.

To dismiss this level and load our regular game we need to display a button on the screen that the user can click on and we need to listen for activity on this button. UnityGUI provides a clean mechanism to do this quickly, albeit not with the best performance. Ways to improve this performance will be covered in the Performance and Optimization sections later in the book.

using System.Collections;
using UnityEngine;

public class SplashGUI : MonoBehaviour{

  void OnGUI(){
    if ( GUI.Button(new Rect(10,10,100,25), "I Understand") ) {
      Application.LoadLevel("MainGame");
    }
  }
}

Now when the user clicks on the button with the I Understand label, the application will load the level MainGame and our game will continue on without any further interruption.

What just happened?

When Unity started the player on our iOS device, it populated the iPhoneSettings class with the settings for our device. Since our script was attached to an object in the scene, our script had the ability to read these settings from the Unity player on the device. Based upon this we used a simple C# conditional if statement to determine if we were fine to start or needed to display a standard definition splash screen. We then presented a simple UnityGUI script that waited for the user to press a button which launched the main game.

Location services

The location services provide an interface to the cell tower triangulation and GPS functions of the device. You can get the status of the services through iPhoneSettings, and the location data will be available through iPhoneInput.lastLocation.

To start the service, access the StartlocationServiceUpdates() method and give it the accuracy that you desire, as well as the update distance, both in meters. The update distance is the amount that the device must move before it will update the iPhoneInput.lastLocation. If you are familiar with using CoreLocation, you are familiar with the location services for iOS as Unity provides a wrapper around the CoreLocation API and simply exposes the data updates through iPhoneInput.lastLocation.

It is important to note, however, that GPS satellite acquisition is not instantaneous and Unity will expose the state of CoreLocations's response in the LocationServiceStatus attribute. This will return one of several LocationServiceStatus states which can be polled until you achieve a LocationServiceStatus. Running in the field at which point you will have actual GPS data in iPhoneInput.lastLocation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.184.189