(For more resources related to this topic, see here.)
Creating collectibles to save
The Unity engine features an incredibly simple saving and loading system that can load your data between sessions in just a few lines of code. The downside of using Unity’s built-in data management is that save data will be erased if the game is ever uninstalled from the OUYA console. Later, we’ll talk about how to make your data persistent between installations, but for now, we’ll set up some basic data-driven values in your marble prototype. However, before we load the saved data, we have to create something to save.
Time for action – creating a basic collectible
Some games use save data to track the total number of times the player has obtained a collectible item. Players may not feel like it’s worth gathering collectibles if they disappear when the game session is closed, but making the game track their long-term progress can give players the motivation to explore a game world and discover everything it has to offer. We’re going to add collectibles to the marble game prototype you created and save them so that the player can see how many collectibles they’ve totally gathered over every play session. Perform the following steps to create a collectible:
Open your RollingMarble Unity project and double-click on the scene that has your level in it.
Create a new cylinder from the Create menu in your Hierarchy menu. Move the cylinder so that it rests on the level’s platform. It should appear as shown in the following screenshot:
We don’t want our collectible to look like a plain old cylinder, so manipulate it with the rotate and scale tools until it looks a little more like a coin. Obviously, you’ll have a coin model in the final game that you can load, but we can customize and differentiate primitive objects for the purpose of our prototype.
Our primitive is starting to look like a coin, but it’s still a bland gray color. To make it look a little bit nicer, we’ll use Unity to apply a material.
A material tells the engine how an object should appear when it is rendered, including which textures and colors to use for each object. Right now, we’ll only apply a basic color, but later on we’ll see how it can store different kinds of textures and other data.
Materials can be created and customized in a matter of minutes in Unity, and they’re a great way to color simple objects or distinguish primitive shapes from one another.
Create a new folder named Materials in your Project window and right-click on it to create a new material named CoinMaterial as shown in the following screenshot:
Click on the material that you just created and its properties will appear in the Inspector window. Click on the color box next to the Main Color property and change it to a yellow color. The colored sphere in the Material window will change to reflect how the material will look in real time, as shown in the following screenshot:
Our collectible coin now has a color, but as we can see from the preview of the sphere, it’s still kind of dull. We want our coin to be shiny so that it catches the player’s eye, so we’ll change the Shader type, which dictates how light hits the object.
The current Shader type on our coin material is Diffuse, which basically means it is a softer, nonreflective material. To make the coin shiny, change the Shader type to Specular. You’ll see a reflective flare appear on the sphere preview; adjust the Shininess slider to see how different levels of specularity affect the material.
You may have noticed that another color value was added when you changed the material’s shader from Diffuse to Specular; this value affects only the shiny parts of the object. You can make the material shine brighter by changing it from gray to white, or give its shininess a tint by using a completely new color.
Attach your material to the collectible object by clicking-and-dragging the material from the Project window and releasing it over the object in your scene view. The object will look like the one shown in the following screenshot:
Our collectible coin object now has a unique shape and appearance, so it’s a good idea to save it as a prefab.
Create a Prefabs folder in your Project window if you haven’t already, and use the folder’s right-click menu to create a new blank prefab named Coin. Click-and-drag the coin object from the hierarchy to the prefab to complete the link.
We’ll add code to the coin later, but we can change the prefab after we initially create it, so don’t worry about saving an incomplete collectible. Verify whether the prefab link worked by clicking-and-dragging multiple instances of the prefab from the Project window onto the Scene view.
What just happened?
Until you start adding 3D models to your game, primitives are a great way to create placeholder objects, and materials are useful for making them look more complex and unique.
Materials add color to objects, but they also contain a shader that affects the way light hits the object. The two most basic shaders are Diffuse (dull) and Specular (shiny), but there are several other shaders in Unity that can help make your object appear exactly like you want it. You can even code your own shaders using the ShaderLab language, which you can learn on your own using the documentation at http://docs.unity3d.com/Documentation/Components/SL-Reference.html.
Next, we’ll add some functionality to your coin to save the collection data.
Have a go hero – make your prototype stand out with materials
As materials are easy to set up with Unity’s color picker and built-in shaders, you have a lot of options at your fingertips to quickly make your prototype stand out and look better than a basic grayscale mock-up. Take any of your existing projects and see how far you can push the aesthetic with different combinations of colors and materials.
Keep the following points in mind:
Some shaders, such as Specular, have multiple colors that you can assign. Play around with different combinations to create a unique appearance.
There are more shaders available to you than just the ones loaded into a new project; move your mouse over the Import Package option in Unity’s Assets menu and import the Toon Shading package to add even more options to your shader collection.
Complex object prefabs made of more than one primitive can have a different material on each primitive. Add multiple materials to a single object to help your user differentiate between its various parts and give your scene more detail.
Try changing the materials used in your scene until you come up with something unique and clean, as shown in the following screenshot of our cannon prototype with custom materials: