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.
You're reading from Ouya Unity Game Development
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 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.
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...
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...
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...
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.
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
.
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.
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.