In this tutorial, you will learn how to create a main menu for a Unity game. That menu will be contained in its own scene.
Having a menu enables us to select if we want to play multiplayer or with AI. Actually, it enables us to do any kind of pre-gameplay setup.
There are a few problems though! How are we going to communicate between scenes? Enough talking and let’s get into it!
This post contains all the code that’s been written in this YouTube video.
GameValues.cs
public static class GameValues { public static bool IsMultiplayer; }
MenuManager.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class MenuManager : MonoBehaviour { public void PlayGame() { SceneManager.LoadScene("main"); } public void SetMultiplayer(bool isOn) { GameValues.IsMultiplayer = isOn; } }
PlayerController.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { public List<PlayerMovement> Players = new List<PlayerMovement>(); public GameObject AiPlayer; private void Start() { if (GameValues.IsMultiplayer) { AiPlayer.GetComponent<PlayerMovement>().enabled = true; AiPlayer.GetComponent<AiScript>().enabled = false; } else { AiPlayer.GetComponent<PlayerMovement>().enabled = false; AiPlayer.GetComponent<AiScript>().enabled = true; } } // Update is called once per frame void Update () { for (int i = 0; i < Input.touchCount; i++) { Vector2 touchWorldPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position); foreach (var player in Players) { if (player.LockedFingerID == null) { if (Input.GetTouch(i).phase == TouchPhase.Began && player.PlayerCollider.OverlapPoint(touchWorldPos)) { player.LockedFingerID = Input.GetTouch(i).fingerId; } } else if (player.LockedFingerID == Input.GetTouch(i).fingerId) { player.MoveToPosition(touchWorldPos); if (Input.GetTouch(i).phase == TouchPhase.Ended || Input.GetTouch(i).phase == TouchPhase.Canceled) player.LockedFingerID = null; } } } } }
UiManager.cs
using UnityEngine; using UnityEngine.SceneManagement; public class UiManager : MonoBehaviour { [Header("Canvas")] public GameObject CanvasGame; public GameObject CanvasRestart; [Header("CanvasRestart")] public GameObject WinTxt; public GameObject LooseTxt; [Header("Other")] public AudioManager audioManager; public ScoreScript scoreScript; public PuckScript puckScript; public PlayerMovement playerMovement; public AiScript aiScript; public void ShowRestartCanvas(bool didAiWin) { Time.timeScale = 0; CanvasGame.SetActive(false); CanvasRestart.SetActive(true); if (didAiWin) { audioManager.PlayLostGame(); WinTxt.SetActive(false); LooseTxt.SetActive(true); } else { audioManager.PlayWonGame(); WinTxt.SetActive(true); LooseTxt.SetActive(false); } } public void RestartGame() { Time.timeScale = 1; CanvasGame.SetActive(true); CanvasRestart.SetActive(false); scoreScript.ResetScores(); puckScript.CenterPuck(); playerMovement.ResetPosition(); aiScript.ResetPosition(); } public void ShowMenu() { Time.timeScale = 1; SceneManager.LoadScene("menu"); } }
This is great! With codes and all. This is invaluable!