Start menu in Unity - unity ui

 How to make Start menu in Unity - unity ui

Start menu in Unity - unity ui


in This tutorial you will know how to create the following buttons   :

  1. Add play button .
  2. Add Setting button .
  3. Add sound button Controller .
  4. Add Quit button .
  5. Add Help button .
  6. Add Share button .
  7. Add More Game Button .
Anwar's Adventure : Escape The Factory

  • first thing to do is designing your menu for The Home Scene .
  • create a new gameObgect ,name it " MainMenuController "
  • add a new script to the gameObgect MainMenuController and name itMainMenuController "
main menu unity


  • This script   MainMenuController .cs control the main menu buttons and animations of The Home Scene .
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;



public class MainMenuController : MonoBehaviour {

   

    // Use this for initialization
    void Start ()
    {
       
    }
   
    // Update is called once per frame
    void Update ()
{
   
    }

   
}

  • we get the ref to our setting button animator  and we have serialized field because the variable is private and we want to access it from inspector :
    [SerializeField]
    private Animator settingButtonAnim;

  • this bool tell wheater the buttons setting button holder are hidden or not .
   private bool hidden;

  • this prevent the user from touching the setting button when the setting button animation is playing .
    private bool canTouchSettingButtons;


  • ref to music button and its sprite .
    [SerializeField]
    private Button musicBtn;
    [SerializeField]
    private Sprite[] musicBtnSprite;

    private AudioSource clickSound;


  • at start we check the music status and the assign the sprite to the music button and vol to game .
    // Use this for initialization
    void Start ()
    {
        //at start we check the music status and the assign the sprite to the music button and vol to game
        if (GameManager.singleton.isMusicOn)
        {
            AudioListener.volume = 1;
            musicBtn.image.sprite = musicBtnSprite[1];
        }
        else
        {
            AudioListener.volume = 0;
            musicBtn.image.sprite = musicBtnSprite[0];
        }

        clickSound = GetComponent<AudioSource>();

        //at start we want this bool to be true
        canTouchSettingButtons = true;
        hidden = true;
    }

  • in Update() is check if button is click and if yes then we play slideIn animation and wait for 1.2 sec and the we again make the button interactable .


    //this is used to control the animation of setting button
    IEnumerator DisableWhilePlayingSettingAnim()
    {
        //is check if button is click
        if (canTouchSettingButtons)
        {
            //we check if the buttons are hidden
            if (hidden)
            {
                //if yes then we play slideIn animation and wait for 1.2 sec and the we again make the button interactable
                canTouchSettingButtons = false;
                settingButtonAnim.Play("SlideIn");
                hidden = false;
                yield return new WaitForSeconds(1.2f);
                canTouchSettingButtons = true;
            }
            else
            {
                canTouchSettingButtons = false;
                settingButtonAnim.Play("SlideOut");
                hidden = true;
                yield return new WaitForSeconds(1.2f);
                canTouchSettingButtons = true;
            }


        }
    }


  • The method which we will assign to setting button . when setting button is clicked we start out coroutine .

    public void SettingBtn()
    {
        //when setting button is clicked we start out coroutine
        StartCoroutine(DisableWhilePlayingSettingAnim());
        clickSound.Play();
    }
 
  • The method which we will assign to play button .
    public void PlayButton()
    {
        //Application.LoadLevel("ModeSelector"); // if you are using unity below 5.3 version
        SceneManager.LoadScene("ModeSelector");//use this for 5.3 version
        clickSound.Play();
    }


  • The method which we will assign to Quit Button :
    public void QuitButton()
    {
        Application.Quit();
    }

  • The method which we will assign to music button :
    public void MusicButton()
    {
        //it check the music status wheather its on of not and when we click the button it make is on or off respectively
        clickSound.Play();

        if (GameManager.singleton.isMusicOn)
        {
            AudioListener.volume = 0;
            musicBtn.image.sprite = musicBtnSprite[0];
            GameManager.singleton.isMusicOn = false;
            GameManager.singleton.Save();
        }
        else
        {
            AudioListener.volume = 1;
            musicBtn.image.sprite = musicBtnSprite[1];
            GameManager.singleton.isMusicOn = true;
            GameManager.singleton.Save();
        }
    }

menu Settings - Sound On / Off - rate -more games

  • The method which we will assign to more games button :
    public void MoreGameButton()
    {
        //here you can provide link of your other games
        Application.OpenURL("http://sfgames.epizy.com/");
        clickSound.Play();
    }

  • The method which we will assign to  info button
    public void InfoButton()
    {//this is to provide the info on how to play game
        clickSound.Play();
    }

  • method which we will assign to rate button :
    public void RateButton()
    {
        //here provide the link of your game so player can rate it
        Application.OpenURL("http://sfgames.epizy.com/2020/11/14/maths-game/");
        clickSound.Play();
    }


  • now add a Audio Source to the MainMenuController gameObgect and make like this :







  • To know how to  make Share Button click here

...
créer un menu unity 2d,script menu unity,unity pause menu,unity ui tutorial 2020,button unity,unity ui builder,unity save,main menu ui,créer un menu unity 2d,script menu unity,unity pause menu,unity ui tutorial 2020,button unity,unity ui builder,