Creating A Main Menu

確認済のバージョン: 5.3

-

難易度: 初級

Learn how to use Unity's UI system to create a basic menu for your game including buttons, sub-pages for help and audio settings and scripts to control menu functionality. Download the asset package used here .

Creating A Main Menu

初級 User Interface (UI)

Time code reference:
Clicking links will open a new tab at linked time in video.

2:20 Import Assets
3:40 Set Up Background
5:20 Create Canvas & Panel
7:18 Anchor Panel Position
10:05 Adding Art To Our Panel
12:28 Importing and Slicing Your Own Sprites For UI
16:20 Adding Buttons
17:30 Button Transition with Color Tint
21:35 Vertical Layout Group
27:32 Creating Audio Panel
31:05 Creating Help Panel
32:49 Changing Canvas Scale
33:50 Loading Scene From Start Button
40:20 Help Button To Open Help Panel
44:00 Audio Button To Open Audio Panel
44:44 Back Buttons To Exit To Main Menu
47:47 Quit Button To Quit Game / Exit Play Mode
52:10 Detecting Keyboard or Gamepad Navigation and Selecting UI

LoadSceneOnClick

Code snippet

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class LoadSceneOnClick : MonoBehaviour {

    public void LoadByIndex(int sceneIndex)
    {
        SceneManager.LoadScene (sceneIndex);
    }
}

QuitOnClick

Code snippet

using UnityEngine;
using System.Collections;

public class QuitOnClick : MonoBehaviour {

    public void Quit()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit ();
#endif
    }

}

SelectOnInput

Code snippet

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class SelectOnInput : MonoBehaviour {

    public EventSystem eventSystem;
    public GameObject selectedObject;

    private bool buttonSelected;

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () 
    {
        if (Input.GetAxisRaw ("Vertical") != 0 && buttonSelected == false) 
        {
            eventSystem.SetSelectedGameObject(selectedObject);
            buttonSelected = true;
        }
    }

    private void OnDisable()
    {
        buttonSelected = false;
    }
}

関連するチュートリアル