Launch from the main menu

Now that our game is starting to work, we should make sure we can go from the Menu scene to the Game scene without any problem.

Open the Menu scene now. We'll make sure the main menu's background texture fades out after the Game scene is loaded, and the main menu's UI camera is disabled to ensure no elements are displayed during the game.

Open our MenuManager.cs script and add the following global variable declarations:

// We'll need the UI camera
public Camera uiCamera;
// We'll need the UI background
public GameObject background;

Save the script and go back to Unity. We'll assign these two new variables now:

  1. Select our UI Root GameObject. For its attached Menu Manager:
    • Drag UI Root | Camera into its UI Camera field.
    • Drag UI Root | Background into the Background field.

Go back to the MenuManager.cs script and add the following EnterGame() coroutine:

// EnterGame Coroutine
private IEnumerator EnterGameRoutine()
{
  // Fade out the UI background
  TweenAlpha.Begin(background, 0.5f, 0);
  // Wait for the tween to finish
  yield return new WaitForSeconds(0.5f);
  // Hide the main menu UI by disabling cam
  uiCamera.enabled = false;
}

Now, we need to start that coroutine when the Game scene is loaded. Add the following lines in the LoadGameScene() method of MenuManager.cs, just below the Application.LoadLevel("Game") line:

// Launch the enter game routine
StartCoroutine("EnterGameRoutine");

Hit Unity's play button. Click on our main menu's Play button to launch the game. The background now fades out, and the UI camera is disabled after the fade.

Great! Now, let's summarize what you've learned in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.205.27