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 3. Setting Up Your Game

After the history lesson and setting up your project you must be looking forward to do some actual development. In this chapter, we'll be explaining the difference between three of the languages you can use to program in Unity, creating a basic structure for our game, creating a title screen, creating an in-game screen, and rendering a level.

Boo, C#, or UnityScript


Unity is amazing. With minimal effort you can have a prototype up and running in a few hours. No other engine has allowed such great flexibility. Another of Unity's excellent features is the ability to program in three languages, namely C#, UnityScript, and Boo. Let's go in to the pros and cons of each one, and explain which one we'll be using.

Boo

Boo is an object-oriented, statically-typed programming language that has a syntax inspired by Python. Boo has only been covered here for completeness, the documentation for programming in Unity with Boo is poor to non-existent and only around 5 percent of Unity developers actually use Boo, so finding support when you need it could prove difficult.

UnityScript

If you know JavaScript then you'll feel comfortable with UnityScript. The syntax is exactly the same; you just need to learn all the addressable objects from Unity. Both JavaScript and UnityScript support dynamic typing. This is great if you are just trying to create...

The project structure


Keeping your Unity project in a good shape is important. Things can quickly get out of hand as a project goes from small prototype to full on work-in-progress. Normally, you have good intentions to tidy it up at a later date but the deadlines and bugs always seem to get in the way. Setting up the project structure before you start, and sticking to it as you go along, will save you a lot of time as you won't be searching for files later on when you could be programming.

You should still have your Sokoban project from Chapter 2, Setting Up Unity and the Ouya Plug-in, so we'll carry on from that point. So far the project is set up so that the platform is Android and we have imported the OuyaSDK-Core unity package. This will have created some folders already in your project. Make the project panel visible by clicking on Window | Project; there should already be some folders there, namely LitJson, Ouya, and Plugins. We're going to create some new folders to hold other files...

Setting up your Scenes


You should now have a project panel with seven folders and a scene with Main Camera and OuyaGameObject that you have entered your developer ID into. We are going to use this scene as a setup scene that isn't displayed for long but instead adds all the required game objects. Normally in Unity, when loading a new scene, all objects are destroyed, and then the objects in the new scene are loaded. If it is needed to have an object persist between scenes then there is a method, DontDestroyOnLoad that you can pass an object to. If the object is a component or GameObject then its entire transform hierarchy will not be destroyed either. A side effect of this is that if the scene has the GameObject added to it in Unity and you load it multiple times (on a level select screen for example) then the GameObject will be cloned multiple times and, with a GameObject that handles controller input for example, this can result in multiple controller presses being triggered. We can avoid...

Scripts and MonoDevelop


Click on GameObject | Create Empty and a new GameObject will appear on the stage with the name GameObject. It should already be highlighted, so click it once more and the name will become editable, call it AdvanceToNextLevel.

We're going to create and attach a C# sharp script to the AdvanceToNextLevel GameObject that will load a new scene as soon as this one is initialized. Right-click on the Scripts folder you created earlier and then click on Create and select the C# Script label. Once you click on it, a script will appear in the Scripts folder and it should already have focus and be asking you to type a name for the script, call it AdvanceToNextLevel. Double-click on the script in Unity and it will open MonoDevelop, which is an open source, integrated development environment that runs on Linux, Mac OSX, and Windows. It supports automatic code completion, source control, and Graphical User Interface (GUI).

After MonoDevelop has loaded, you will be presented with the...

Scene progression


We have to add the scenes that we're going to use to the build settings before we can test our code works, so click on File | Build Settings, and then click on Add Current. You'll see the scene name appear in the Scenes In Build area, now drag the TitleScreen and the GameScreen scenes into the same window and their names will appear there too. Close the build settings window and click on the play icon in the top-middle of the Unity screen. If all has worked as expected you should be staring back at a plain blue screen. That's actually a good result as it means your SetUp scene is working as expected and has loaded the TitleScreen scene immediately, and the blue screen you see is from the background color of Main Camera in that scene.

Tip

Make sure you click on the play icon again to stop the play mode, this is important as any changes you make in play mode are not permanent and will reset to the default values when play mode is disabled.

The title screen menu

Double-click on...

Creating the level


Start by opening the GameScreen scene you created earlier by double-clicking on it. There should just be a Main Camera and nothing else. Make the background color of the Main Camera black, just like you did earlier, and set the position to X: 3, Y: 6, Z: -10.

Prefabs

A prefab is a type of asset, a reusable GameObject that is accessible from the Project panel. Prefabs can be added into any scene, multiple times per scene. When you add a prefab to a scene, an instance is created of it. All prefab instances are linked to the original prefab. No matter how many instances exist in your project, when you make a change to the prefab, you will see the change applied to all instances of it.

Creating a Prefab

In order to create a prefab, drag a GameObject that you've created in the scene into the Project panel, we're using the Prefabs folder to store ours. The GameObject's name will turn blue to show that it is a prefab.

We're going to need five prefabs in our game; a wall, a floor,...

Summary


We've laid a lot of groundwork in this chapter, so well done. You should now understand the different coding options available to you and their pros and cons and your Unity project should also be set up in a way that will help you work smarter in the future. We also achieved our first Ouya specific programming with the controller input. In Chapter 4, Adding a Character and Making Them Move, we'll give you a better camera angle, make your character move and add some animation.

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