Gigi Labs

Please follow Gigi Labs for the latest articles. Programmer's Ranch no longer has its domain, so please update your bookmarks and links to

Tuesday, June 4, 2013

Unity3D: Scenes and Building

Hi people! :)

Today we're going to learn about scenes in Unity. Scenes allow us to have different levels, both for gameplay as well as to incorporate things like the game menu, credits screen, etc.
  • Create a new project in Unity, and press Ctrl+S to save your first scene. Call it "Level1".
  • Bring a cube into the scene via GameObject menu -> Create Other -> Cube.
  • Then, right click in the Assets panel and select Create -> C# Script. Call it "NextLevel".
  • Drag the "NextLevel" script onto the cube.
  • Double-click the "NextLevel script to open it in MonoDevelop.
In the Update() method, put this:

        if (Input.GetMouseButtonDown(0))
            int levelCount = Application.levelCount;
            int currentLevel = Application.loadedLevel;
            if (currentLevel < levelCount - 1)
                Application.LoadLevel(currentLevel + 1);

This will allow you to move to the next level (if there is one) when the user clicks the left mouse button. I'll explain this in a minute.

First, let's create another two scenes (levels). You can do this either via the File menu -> New Scene, or else by selecting a scene in your Assets panel, and pressing Ctrl+D to duplicate it. When you double-click another scene in the Assets panel, Unity will open that scene instead of your old one, so make sure you always save before changing scene.

Create another two scenes using whichever method you prefer. Put a sphere in level 2, and a cylinder in level 3. Drag the "NextLevel" script onto each of these objects. In each scene, adjust the position of the objects so that they are visible to the camera.

Once you are ready, it's time to set up the order of the scenes in the game. Go to File -> Build Settings...:

From the Assets panel, drag the scenes into the "Scenes in Build" area of the Build Settings window:

The scenes are automatically ordered, as you can see from the corresponding number on the right. This means that Level1 is the first scene to be loaded, and the next one is Level2. You can reorder the scenes by dragging them up or down, and you can remove them by selecting them and pressing Delete.

Now, you can test this by opening Level1 in Unity and pressing Play. Level1 (the one with the cube) is loaded first. By clicking the left mouse button, you can transition to the two other scenes.

Back to the code above, Application.levelCount gives you the total number of scenes in your game, in this case 3 (note that they must be listed in the Scenes in Build for them to be considered). Application.loadedLevel gives you the index of the currently loaded level (the ones on the right hand side of Scenes in Build, starting from zero). Application.LoadLevel allows you to transition to any scene you want, by specifying its index.

For the record, it is also very easy to reload the current scene in cases where the player wants to restart the current level.

Finally, the Build Settings screen is also the place to go if you want to distribute your game. From here, you can create executable versions of the game. There are many supported platforms, and recently Unity is also allowing developers to build on iOS and Android for free - something that until recently would have cost several hundred dollars.

In summary, scenes are a great way to split up your game into several different levels that don't depend on each other. You can use them for different game levels, a main menu, credits/help screens, etc. The Application class provides methods and properties allowing you to work with scenes.

Thanks for reading, and stay tuned for more! :)

No comments:

Post a Comment