Reader small image

You're reading from  Ouya Unity Game Development

Product typeBook
Published inOct 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781783559701
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Gary Riches
Gary Riches
author image
Gary Riches

Gary Riches is a longstanding member of the iOS developer community. He has a keen interest not only in established sections of the industry such as gaming but also in emerging technologies such as Ouya, GameStick, and others. Filled with a passion to program on new systems, he has just become a registered Wii U developer and will also create content for Xbox One and PlayStation 4. To target so many platforms he uses Unity, which he learned while working on the Augmented Reality SBook for Saddington Baynes. When not building software for other companies, he builds his own business by creating photo manipulation apps such as Zombify Me, games such as Aztec Antics and Amazed, and also works on educational apps and games such as Nursery Rhymes: Volume 1, 2, and 3.
Read more about Gary Riches

Right arrow

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...

Removing In-App Purchases


The Ouya SDK makes In-App Purchase relatively simple; it integrates nicely with Unity out of the box. Android, on the other hand, can be a pain to integrate with In-App Purchase inside Unity unless you use a plugin from the Asset Store. For this reason, we'll be modifying our Android game to not use In-App Purchase and instead make it work like a full game purchased from one of the many Android app stores.

As we already have the code for giving the player all the levels when we have purchased the game on the Ouya, the simplest way to remove the In-App Purchase requirement would be to set the purchased int in PlayerPrefs when the game starts.

Double-click on your Sokoban script, as we're going to modify our Awake method. Add the following code to the top of it:

#if UNITY_ANDROID
if (OuyaSDK.IsOUYA () == false){
  PlayerPrefs.SetInt("purchased", 1);
}
#endif

The preceding code is checking if we are on Android but not on Ouya, and if that condition is met then we set...

Mobile controls


We're going to keep our controls really simple for this demo, there won't be a virtual joystick or button in sight. As we only need three functions, namely left, forward, and right, we'll be breaking the screen up in to three sections and using the left section for turning left, the center section for forward, and the right section for turning right. The following is a screenshot of the game with the control areas overlaid for illustration purposes:

We already have our control script for the game, Sokoban, so go ahead and double-click on it to edit it in MonoDevelop.

We're going to create a new method that will return true or false when we pass it in a string for the control direction we want to check. It should only return true if the tap has just happened, and not keep returning true if the user holds his finger on the screen. The method is as follows:

  // Check if an area of the screen has been
  // touched for the very first time
  bool FirstTouchForControlType(string controlType...

Summary


Unity will output to iOS, OS X, Android, Windows Phone 8, Windows 8, Blackberry 10, and many other platforms. Organization of your code and project is paramount; otherwise things will get very messy very quickly. Why don't you try making this game work on some other platforms? The best way to learn is through experimenting and performing, so what are you waiting for? If you do get stuck, the forums at http://forum.unity3d.com are really helpful. Hopefully you feel comfortable with the basics of programming on Unity for Ouya now and I look forward to see all the cool new things you come up with. This demo alone could be expanded to have hundreds of levels, more sounds, and some better graphical effects.

My Twitter username is @Gary_BBGames and I'm happy to answer any questions or help out in any way possible if you get stuck. If you create any apps or games, do let me know and I'll help promote them for you.

Thank you for taking the time to go through this book and remember, it's all...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Ouya Unity Game Development
Published in: Oct 2013Publisher: PacktISBN-13: 9781783559701
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Gary Riches

Gary Riches is a longstanding member of the iOS developer community. He has a keen interest not only in established sections of the industry such as gaming but also in emerging technologies such as Ouya, GameStick, and others. Filled with a passion to program on new systems, he has just become a registered Wii U developer and will also create content for Xbox One and PlayStation 4. To target so many platforms he uses Unity, which he learned while working on the Augmented Reality SBook for Saddington Baynes. When not building software for other companies, he builds his own business by creating photo manipulation apps such as Zombify Me, games such as Aztec Antics and Amazed, and also works on educational apps and games such as Nursery Rhymes: Volume 1, 2, and 3.
Read more about Gary Riches