Talking about Circle Chain game, C#, Game development and Unity3D.
Every game framework has – or should have – a way to handle different parts of the your games, freeing memory when switching among them. For instance, when you click “play” on a game main menu, the whole main menu is automatically destroyed from the framework and you can concentrate on the game itself rather than dealing with garbage collection.
In Phaser, we have states, which I explained in the post Phaser Tutorial: understanding Phaser states, and in Unity3D we have scenes.
Let’s see how to add scenes and switching from one scene to another, adding a splash screen to the prototype I showed you in the posts Tutorial: your first Unity2D project – a Circle Chain prototype and Unity2D Circle Chain prototype step 2: adding explosions, so I recommend you to go through these posts before reading this tutorial, if you aren’t used to Unity.
Once you create a new game, a new scene is automatically created and it’s called – guess how – “scene”.
So the first thing we have to do is to add a new scene. Unlike every other resource you can create/import with Unity, new scenes are created from File menu:
Once the scene is created, let’s save it to see what happens:
Choosing File -> Save Scene as you can assign your scene a name, and as you can see by default it’s saved in the same folder where you can find the default scene Unity created when setting up the new game.
At this time, in the same way you created a C# script and added it to scene camera in the making of the first step, create a new C# script with this content:
using UnityEngine; using System.Collections; public class newscenebehavior : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Input.GetButtonDown("Fire1")) { Application.LoadLevel ("scene"); } } void OnGUI () { GUI.Box(new Rect(50,230,400,40), "Click anywhere to begin\nI am just a string"); } }
Line 13 will load the scene called “scene” – which is the scene created in previous steps with the game, once the player clicks anywhere on the stage.
Line 18 just prompts a message on the screen.
When building the project, include all scenes your game should be made of, like in this snapshot:
Also, keep in mind the upper scene will be the first to be shown so you may want to invert the default order:
And this is the final project:
Click on the splash screen scene to start the game scene.
This is the correct way to manage your game scenes saving memory. Download the source code of the entire project.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.