Chapter 7. Building Cross-platform Games

One of Unity's strongest features is its write-once, publish-everywhere functionality. Our game isn't quite ready for that, but with very little work, we'll have it running on Android devices.

The Ouya controller functionality we added earlier already supports PS3 controllers paired to an Android device but we should add some touch screen controls too.

Although we're focusing on Android, the game will run on any device that Unity supports, but the setup of all those development environments requires a book in itself.

Note

This chapter explains how to get the game running on Android. To test your code, you will need an Android phone or tablet. If you don't have one, you won't be able to test the code, but it is still beneficial to read the chapter.

Platform Dependent Compilation

Unity includes a feature called Platform Dependent Compilation. It consists of some preprocessor directives that let you partition your scripts to compile and execute sections of code for one of the supported platforms. This functionality is also supported within Editor, so you can compile the code specifically for your mobile or console and test it in Editor.

This is useful if you're branching code for things, such as In-App Purchase or control mechanisms. To use the Platform Dependent Compilation feature, you use the pound or hash symbol, as shown in the following code:

void Start () {
  
  #if UNITY_EDITOR
    Debug.Log("Unity Editor");
  #endif
  
  #if UNITY_ANDROID
    Debug.Log("Android");
  #endif
  
  #if UNITY_IPHONE
    Debug.Log("iPhone");
  #endif
  
  #if UNITY_STANDALONE_OSX
    Debug.Log("Stand Alone OSX");
  #endif
  
  #if UNITY_STANDALONE_WIN
    Debug.Log("Stand Alone Windows");
  #endif
}

While this is brilliant for coding between platforms, it doesn't actually help in our example. The Ouya and an Android phone might seem different, but they both run on the Android operating system, even though the Ouya does a good job at hiding that. It means our platform-dependent compilation is going to trigger for Android in both scenarios. Thankfully, the Ouya SDK thought of this and provided a method we can call instead. The method is OuyaSDK.IsOUYA() and it is called as shown in the following code:

if (OuyaSDK.IsOUYA()) {
  
  // Remove the In-App Purchase here as 
  // it's not needed for mobile
}

Changing the TitleScreen scene

Currently our title screen has Press O to Play and Press A to Purchase on it, neither of these will apply to the Android version of the game, so let's add something a little more fitting. How about Tap to Start? We're going to have three 3D text GameObjects on our screen and we'll need to toggle the visibility depending on if we are or are not on Ouya. The best way to do this is with a simple script. Create a new C# script and call it ToggleVisibilityForOuya. At the top of the script, where variables are defined, add the following code:

public bool visibleIfOuya;

Change the Start method to an Awake method and change it to the following code:

void Awake () {
  if (OuyaSDK.IsOUYA()) {
    this.gameObject.SetActive(visibleIfOuya);
  } else if (!OuyaSDK.IsOUYA()) {
    this.gameObject.SetActive(!visibleIfOuya);
  }
}

The preceding code will make sure that when we run on the Ouya with the visibleIfOuya script, a Boolean set will show up and if we're not running on the Ouya then the opposite will show up. Now perform the following steps:

  1. Save your script and go back to Unity. Rather than creating a new 3D text and setting all the parameters again, we can duplicate one of the existing ones. Make sure you're editing the TitleScreen scene, click on the Play Instructions GameObject in the Hierarchy panel, and then click on Edit | Duplicate.
  2. Now you'll have two copies of Play Instructions, change the name of one of them to Tap to Play.
  3. While it is selected, move over to the Inspector panel and change the position of Transform to Y to 0, and the text of Text Mesh to Tap to Play.
  4. Click and drag your ToggleVisibilityForOuya script onto all three of the 3D text GameObjects, namely Play Instructions, Purchase Instructions, and Tap to Play.
  5. Click on Play Instructions and, in the Inspector panel, tick the Visible If Ouya box.
  6. Click on Purchase Instructions, and in the Inspector panel, tick the Visible If Ouya box.
  7. Click on Tap to Play, and in the Inspector panel, make sure the tick box is unticked.

Run the game on your Android device and the title screen should just have Tap to Play on. Let's add some code to make that actually happen. Double-click on your ControlsTitleScreen script to edit it in MonoDevelop and change the Update method as follows:

void Update () {
  
  #if UNITY_ANDROID
  if (OuyaSDK.IsOUYA ()) {
    // Update the controllers here for best results
    OuyaInput.UpdateControllers();
    
    if (Input.GetKeyDown(KeyCode.Space) || OuyaInput.GetButtonDown(OuyaButton.O, player)) {
      Application.LoadLevel("GameScreen");
    } else 
    if (OuyaInput.GetButtonDown(OuyaButton.A, player)) {
      
      GameObject iap = GameObject.Find("IAP");
      OuyaShowProducts showProductsScript = iap.GetComponent<OuyaShowProducts>();
      showProductsScript.BuyFullGame();
    }
  } else {
    if (Input.touchCount >= 1) {
      Application.LoadLevel("GameScreen");
    }
  }
  #endif
}

You can see that we now wrapped all the code in a platform-dependent compilation block, so it will only trigger on Android devices, this means we can implement different control systems for different platforms. We then check if we are running on the Ouya or an Android phone with OuyaSDK.IsOUYA. If it returns false we just check for a tap anywhere on the screen to start the game.

Test the game on your Android device now to verify it's all working as it should for you.

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

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