Unity Learn home
View Tutorial Content
Steps

Recorded Video Sessions on UI

Tutorial
Beginner
3 Hours55 Mins
(8)
Summary
Recorded sessions from Unite Conferences and other live events demonstrate various User Interface (UI) development tips and best practices.
Select your Unity version
Last updated: December 23, 2021
4.x
Language
English

1.The New UI

Adam Buckner walks you through the New UI tools in Unity 4.6, with some examples of usage.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)


2.Creating a scene selection menu

In this training session we'll create a menu using the UI tools from which we can choose and load a scene. We'll do some simple scripting to achieve this including looking at Application.LoadLevel, Object.DontDestroyOnLoad and Application.LoadLevelAdditive.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

Script to load a scene when clicked.
###LoadOnClick
using UnityEngine; using System.Collections; public class LoadOnClick : MonoBehaviour { public GameObject loadingImage; public void LoadScene(int level) { loadingImage.SetActive(true); Application.LoadLevel(level); } }
The game object this script is attached to will not be destroyed when a new scene is loaded.

DontDestroy

using UnityEngine; using System.Collections; public class DontDestroy : MonoBehaviour { // Use this for initialization void Awake () { DontDestroyOnLoad(gameObject); } }
Script to change the music playing after a scene is loaded.

ChangeMusic

using UnityEngine; using System.Collections; public class ChangeMusic : MonoBehaviour { public AudioClip level2Music; private AudioSource source; // Use this for initialization void Awake () { source = GetComponent<AudioSource>(); } void OnLevelWasLoaded(int level) { if (level == 2) { source.clip = level2Music; source.Play (); } } }
Script to add the selected scene without destroying objects from the current scene.

LoadAdditive

using UnityEngine; using System.Collections; public class LoadAdditive : MonoBehaviour { public void LoadAddOnClick(int level) { Application.LoadLevelAdditive(level); } }
Script to load a scene in the background while the current script plays (Pro only).

ClickToLoadAsync

using UnityEngine; using System.Collections; using UnityEngine.UI; public class ClickToLoadAsync : MonoBehaviour { public Slider loadingBar; public GameObject loadingImage; private AsyncOperation async; public void ClickAsync(int level) { loadingImage.SetActive(true); StartCoroutine(LoadLevelWithBar(level)); } IEnumerator LoadLevelWithBar (int level) { async = Application.LoadLevelAsync(level); while (!async.isDone) { loadingBar.value = async.progress; yield return null; } } }

3.Making a generic modal window

In this session we will be making a generic modal window (Yes, No, Maybeso, Cancel) where we can push content and actions to the window from anywhere in our game and have the events work when the buttons are pressed. (Part 1 of 3) Tutor - Adam Buckner
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

BringToFront

using UnityEngine; using System.Collections; public class BringToFront : MonoBehaviour { void OnEnable () { transform.SetAsLastSibling (); } }

ModalPanel

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; // This script will be updated in Part 2 of this 2 part series. public class ModalPanel : MonoBehaviour { public Text question; public Image iconImage; public Button yesButton; public Button noButton; public Button cancelButton; public GameObject modalPanelObject; private static ModalPanel modalPanel; public static ModalPanel Instance () { if (!modalPanel) { modalPanel = FindObjectOfType(typeof (ModalPanel)) as ModalPanel; if (!modalPanel) Debug.LogError ("There needs to be one active ModalPanel script on a GameObject in your scene."); } return modalPanel; } // Yes/No/Cancel: A string, a Yes event, a No event and Cancel event public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) { modalPanelObject.SetActive (true); yesButton.onClick.RemoveAllListeners(); yesButton.onClick.AddListener (yesEvent); yesButton.onClick.AddListener (ClosePanel); noButton.onClick.RemoveAllListeners(); noButton.onClick.AddListener (noEvent); noButton.onClick.AddListener (ClosePanel); cancelButton.onClick.RemoveAllListeners(); cancelButton.onClick.AddListener (cancelEvent); cancelButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.gameObject.SetActive (false); yesButton.gameObject.SetActive (true); noButton.gameObject.SetActive (true); cancelButton.gameObject.SetActive (true); } void ClosePanel () { modalPanelObject.SetActive (false); } }

TestModalWindow

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; // This script will be updated in Part 2 of this 2 part series. public class TestModalWindow : MonoBehaviour { private ModalPanel modalPanel; private DisplayManager displayManager; private UnityAction myYesAction; private UnityAction myNoAction; private UnityAction myCancelAction; void Awake () { modalPanel = ModalPanel.Instance (); displayManager = DisplayManager.Instance (); myYesAction = new UnityAction (TestYesFunction); myNoAction = new UnityAction (TestNoFunction); myCancelAction = new UnityAction (TestCancelFunction); } // Send to the Modal Panel to set up the Buttons and Functions to call public void TestYNC () { modalPanel.Choice ("Do you want to spawn this sphere?", TestYesFunction, TestNoFunction, TestCancelFunction); // modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", myYesAction, myNoAction, myCancelAction); } // These are wrapped into UnityActions void TestYesFunction () { displayManager.DisplayMessage ("Heck yeah! Yup!"); } void TestNoFunction () { displayManager.DisplayMessage ("No way, José!"); } void TestCancelFunction () { displayManager.DisplayMessage ("I give up!"); } }

DisplayManager

using UnityEngine; using UnityEngine.UI; using System.Collections; public class DisplayManager : MonoBehaviour { public Text displayText; public float displayTime; public float fadeTime; private IEnumerator fadeAlpha; private static DisplayManager displayManager; public static DisplayManager Instance () { if (!displayManager) { displayManager = FindObjectOfType(typeof (DisplayManager)) as DisplayManager; if (!displayManager) Debug.LogError ("There needs to be one active DisplayManager script on a GameObject in your scene."); } return displayManager; } public void DisplayMessage (string message) { displayText.text = message; SetAlpha (); } void SetAlpha () { if (fadeAlpha != null) { StopCoroutine (fadeAlpha); } fadeAlpha = FadeAlpha (); StartCoroutine (fadeAlpha); } IEnumerator FadeAlpha () { Color resetColor = displayText.color; resetColor.a = 1; displayText.color = resetColor; yield return new WaitForSeconds (displayTime); while (displayText.color.a > 0) { Color displayColor = displayText.color; displayColor.a -= Time.deltaTime / fadeTime; displayText.color = displayColor; yield return null; } yield return null; } }

4.Making a generic modal window (Pt 2)

In this session we will be making a generic modal window (Yes, No, Maybeso, Cancel) where we can push content and actions to the window from anywhere in our game and have the events work when the buttons are pressed. (Part 2 of 3) Tutor - Adam Buckner
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

BringToFront

using UnityEngine; using System.Collections; public class BringToFront : MonoBehaviour { void OnEnable () { transform.SetAsLastSibling (); } } ```###DeactivateMe###

csharp

using UnityEngine; using System.Collections; public class DeactivateMe : MonoBehaviour { void Awake () { gameObject.SetActive(false); } }

ModalPanel

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; public class ModalPanel : MonoBehaviour { public Text question; public Image iconImage; public Button yesButton; public Button noButton; public Button cancelButton; public GameObject modalPanelObject; private static ModalPanel modalPanel; public static ModalPanel Instance () { if (!modalPanel) { modalPanel = FindObjectOfType(typeof (ModalPanel)) as ModalPanel; if (!modalPanel) Debug.LogError ("There needs to be one active ModalPanel script on a GameObject in your scene."); } return modalPanel; } // Announcement: A string and Cancel event; public void Choice (string question, UnityAction cancelEvent) { modalPanelObject.SetActive (true); cancelButton.onClick.RemoveAllListeners(); cancelButton.onClick.AddListener (cancelEvent); cancelButton.onClick.AddListener (ClosePanel); this.question.text = question; iconImage.gameObject.SetActive(false); yesButton.gameObject.SetActive(false); noButton.gameObject.SetActive(false); cancelButton.gameObject.SetActive(true); } // Announcement with Image: A string, a Sprite and Cancel event; public void Choice (string question, Sprite iconImage, UnityAction cancelEvent) { modalPanelObject.SetActive (true); cancelButton.onClick.RemoveAllListeners(); cancelButton.onClick.AddListener (cancelEvent); cancelButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.sprite = iconImage; this.iconImage.gameObject.SetActive(true); yesButton.gameObject.SetActive(false); noButton.gameObject.SetActive(false); cancelButton.gameObject.SetActive(true); } // Yes/No: A string, a Yes event, a No event (No Cancel Button); public void Choice (string question, UnityAction yesEvent, UnityAction noEvent) { modalPanelObject.SetActive (true); yesButton.onClick.RemoveAllListeners(); yesButton.onClick.AddListener (yesEvent); yesButton.onClick.AddListener (ClosePanel); noButton.onClick.RemoveAllListeners(); noButton.onClick.AddListener (noEvent); noButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.gameObject.SetActive(false); yesButton.gameObject.SetActive(true); noButton.gameObject.SetActive(true); cancelButton.gameObject.SetActive(false); } // Yes/No/Cancel: A string, a Yes event, a No event and Cancel event; public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) { modalPanelObject.SetActive (true); yesButton.onClick.RemoveAllListeners(); yesButton.onClick.AddListener (yesEvent); yesButton.onClick.AddListener (ClosePanel); noButton.onClick.RemoveAllListeners(); noButton.onClick.AddListener (noEvent); noButton.onClick.AddListener (ClosePanel); cancelButton.onClick.RemoveAllListeners(); cancelButton.onClick.AddListener (cancelEvent); cancelButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.gameObject.SetActive(false); yesButton.gameObject.SetActive(true); noButton.gameObject.SetActive(true); cancelButton.gameObject.SetActive(true); } // Yes/No with Image: A string, a Sprite, a Yes event, a No event (No Cancel Button); public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent) { modalPanelObject.SetActive (true); yesButton.onClick.RemoveAllListeners(); yesButton.onClick.AddListener (yesEvent); yesButton.onClick.AddListener (ClosePanel); noButton.onClick.RemoveAllListeners(); noButton.onClick.AddListener (noEvent); noButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.sprite = iconImage; this.iconImage.gameObject.SetActive(true); yesButton.gameObject.SetActive(true); noButton.gameObject.SetActive(true); cancelButton.gameObject.SetActive(false); } // Yes/No/Cancel with Image: A string, a Sprite, a Yes event, a No event and Cancel event; public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) { modalPanelObject.SetActive (true); yesButton.onClick.RemoveAllListeners(); yesButton.onClick.AddListener (yesEvent); yesButton.onClick.AddListener (ClosePanel); noButton.onClick.RemoveAllListeners(); noButton.onClick.AddListener (noEvent); noButton.onClick.AddListener (ClosePanel); cancelButton.onClick.RemoveAllListeners(); cancelButton.onClick.AddListener (cancelEvent); cancelButton.onClick.AddListener (ClosePanel); this.question.text = question; this.iconImage.sprite = iconImage; this.iconImage.gameObject.SetActive(true); yesButton.gameObject.SetActive(true); noButton.gameObject.SetActive(true); cancelButton.gameObject.SetActive(true); } void ClosePanel () { modalPanelObject.SetActive (false); } }

TestModalWindow

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; public class TestModalPanel : MonoBehaviour { public Sprite icon; public Transform spawnPoint; public GameObject thingToSpawn; public DisplayManager displayManager; private ModalPanel modalPanel; // private UnityAction myYesAction; // private UnityAction myNoAction; // private UnityAction myCancelAction; void Awake () { modalPanel = ModalPanel.Instance(); // myYesAction = new UnityAction (TestYesFunction); // myNoAction = new UnityAction (TestNoFunction); // myCancelAction = new UnityAction(TestCancelFunction); } // Send to the Modal Panel to set up the Buttons and functions to call public void TestC () { modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", TestCancelFunction); // modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", myCancelAction); } public void TestCI () { modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", icon, TestCancelFunction); // modalPanel.Choice ("This is an announcement!\nIf you don't like it, shove off!", myCancelAction); } public void TestYN () { modalPanel.Choice ("Cheese on your burger?", TestYesFunction, TestNoFunction); // modalPanel.Choice ("Cheese on your burger?", myYesAction, myNoAction); } public void TestYNC () { modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", TestYesFunction, TestNoFunction, TestCancelFunction); // modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", myYesAction, myNoAction, myCancelAction); } public void TestYNI () { modalPanel.Choice ("Do you like this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction); // modalPanel.Choice ("Do you like this icon?", icon, myYesAction, myNoAction, myCancelAction); } public void TestYNCI () { modalPanel.Choice ("Do you want to use this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction); // modalPanel.Choice ("Do you like this icon?", icon, myYesAction, myNoAction, myCancelAction); } public void TestLambda () { modalPanel.Choice ("Do you want to create this sphere?", () => { InstantiateObject(thingToSpawn); }, TestNoFunction); // modalPanel.Choice ("Do you want to create this sphere?", () => { InstantiateObject(thingToSpawn); }, myNoAction); } public void TestLambda2 () { modalPanel.Choice ("Do you want to create two spheres?", () => { InstantiateObject(thingToSpawn, thingToSpawn); }, TestNoFunction); // modalPanel.Choice ("Do you want to create two spheres?", () => { InstantiateObject(thingToSpawn, thingToSpawn); }, myNoAction); } public void TestLambda3 () { modalPanel.Choice ("Do you want to create three spheres?", () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn); }, TestNoFunction); // modalPanel.Choice ("Do you want to create three spheres?", () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn); }, myNoAction); } // The function to call when the button is clicked // These are wrapped up in a UnityAction during Awake void TestYesFunction () { displayManager.DisplayMessage ("Heck, yeah!"); } void TestNoFunction () { displayManager.DisplayMessage ("No way, Jose!"); } void TestCancelFunction () { displayManager.DisplayMessage ("I give up!"); } void InstantiateObject (GameObject thingToInstantiate) { displayManager.DisplayMessage ("Here you go!"); Instantiate (thingToInstantiate, spawnPoint.position, spawnPoint.rotation); } void InstantiateObject (GameObject thingToInstantiate, GameObject thingToInstantiate2) { displayManager.DisplayMessage ("Here you go!"); Instantiate (thingToInstantiate, spawnPoint.position - Vector3.one, spawnPoint.rotation); Instantiate (thingToInstantiate2, spawnPoint.position + Vector3.one, spawnPoint.rotation); } }

DisplayManager

using UnityEngine; using UnityEngine.UI; using System.Collections; public class DisplayManager : MonoBehaviour { public Text displayText; public float displayTime; public float fadeTime; private IEnumerator fadeAlpha; private static DisplayManager displayManager; public static DisplayManager Instance () { if (!displayManager) { displayManager = FindObjectOfType(typeof (DisplayManager)) as DisplayManager; if (!displayManager) Debug.LogError ("There needs to be one active DisplayManager script on a GameObject in your scene."); } return displayManager; } public void DisplayMessage (string message) { displayText.text = message; SetAlpha (); } void SetAlpha () { if (fadeAlpha != null) { StopCoroutine (fadeAlpha); } fadeAlpha = FadeAlpha (); StartCoroutine (fadeAlpha); } IEnumerator FadeAlpha () { Color resetColor = displayText.color; resetColor.a = 1; displayText.color = resetColor; yield return new WaitForSeconds (displayTime); while (displayText.color.a > 0) { Color displayColor = displayText.color; displayColor.a -= Time.deltaTime / fadeTime; displayText.color = displayColor; yield return null; } yield return null; } }

5.Making a generic modal window (Pt 3)

In this session we will be making a generic modal window (Yes, No, Maybeso, Cancel) where we can push content and actions to the window from anywhere in our game and have the events work when the buttons are pressed. In this particular session, we will look at code options (if not optimizations) and other final touches. (Part 3 of 3) Tutor - Adam Buckner
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

Be aware that ModalPanel.cs is a work in progress and we adapted only a few of the functions in the scripts. The un-adapted code has been commented out. This code is still valid, and will work, but to avoid confusion, only the code used in Pt3 is uncommented.

ModalPanel

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; public class EventButtonDetails { public string buttonTitle; public Sprite buttonBackground; // Not implemented public UnityAction action; } public class EventSliderDetails { } public class ModalPanelDetails { public string title; // Not implemented public string question; public Sprite iconImage; public Sprite panelBackgroundImage; // Not implemented public EventButtonDetails button1Details; public EventButtonDetails button2Details; public EventButtonDetails button3Details; public EventButtonDetails button4Details; public EventSliderDetails sliderDetails; } public class ModalPanel : MonoBehaviour { public Text question; public Image iconImage; public Button button1; public Button button2; public Button button3; public Text button1Text; public Text button2Text; public Text button3Text; public GameObject modalPanelObject; private static ModalPanel modalPanel; public static ModalPanel Instance () { if (!modalPanel) { modalPanel = FindObjectOfType(typeof (ModalPanel)) as ModalPanel; if (!modalPanel) Debug.LogError ("There needs to be one active ModalPanel script on a GameObject in your scene."); } return modalPanel; } // // Announcement with Image: A string, a Sprite and Cancel event; // public void Choice (string question, UnityAction cancelEvent, Sprite iconImage = null) { // modalPanelObject.SetActive (true); // // button3.onClick.RemoveAllListeners(); // button3.onClick.AddListener (cancelEvent); // button3.onClick.AddListener (ClosePanel); // // this.question.text = question; // if (iconImage) // this.iconImage.sprite = iconImage; // // if (iconImage) // this.iconImage.gameObject.SetActive(true); // else // this.iconImage.gameObject.SetActive(false); // button1.gameObject.SetActive(false); // button2.gameObject.SetActive(false); // button3.gameObject.SetActive(true); // } public void NewChoice (ModalPanelDetails details){ modalPanelObject.SetActive (true); this.iconImage.gameObject.SetActive(false); button1.gameObject.SetActive(false); button2.gameObject.SetActive(false); button3.gameObject.SetActive(false); this.question.text = details.question; if (details.iconImage) { this.iconImage.sprite = details.iconImage; this.iconImage.gameObject.SetActive(true); } button1.onClick.RemoveAllListeners(); button1.onClick.AddListener (details.button1Details.action); button1.onClick.AddListener (ClosePanel); button1Text.text = details.button1Details.buttonTitle; button1.gameObject.SetActive(true); if (details.button2Details != null) { button2.onClick.RemoveAllListeners(); button2.onClick.AddListener (details.button2Details.action); button2.onClick.AddListener (ClosePanel); button2Text.text = details.button2Details.buttonTitle; button2.gameObject.SetActive(true); } if (details.button3Details != null) { button3.onClick.RemoveAllListeners(); button3.onClick.AddListener (details.button3Details.action); button3.onClick.AddListener (ClosePanel); button3Text.text = details.button3Details.buttonTitle; button3.gameObject.SetActive(true); } } // // Yes/No: A string, a Yes event, a No event (No Cancel Button); // public void Choice (string question, UnityAction yesEvent, UnityAction noEvent) { // modalPanelObject.SetActive (true); // // button1.onClick.RemoveAllListeners(); // button1.onClick.AddListener (yesEvent); // button1.onClick.AddListener (ClosePanel); // // button2.onClick.RemoveAllListeners(); // button2.onClick.AddListener (noEvent); // button2.onClick.AddListener (ClosePanel); // // this.question.text = question; // // this.iconImage.gameObject.SetActive(false); // button1.gameObject.SetActive(true); // button2.gameObject.SetActive(true); // button3.gameObject.SetActive(false); // } // // // Yes/No/Cancel: A string, a Yes event, a No event and Cancel event; // public void Choice (string question, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) { // modalPanelObject.SetActive (true); // // button1.onClick.RemoveAllListeners(); // button1.onClick.AddListener (yesEvent); // button1.onClick.AddListener (ClosePanel); // // button2.onClick.RemoveAllListeners(); // button2.onClick.AddListener (noEvent); // button2.onClick.AddListener (ClosePanel); // // button3.onClick.RemoveAllListeners(); // button3.onClick.AddListener (cancelEvent); // button3.onClick.AddListener (ClosePanel); // // this.question.text = question; // // this.iconImage.gameObject.SetActive(false); // button1.gameObject.SetActive(true); // button2.gameObject.SetActive(true); // button3.gameObject.SetActive(true); // } // // // Yes/No with Image: A string, a Sprite, a Yes event, a No event (No Cancel Button); // public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent) { // modalPanelObject.SetActive (true); // // button1.onClick.RemoveAllListeners(); // button1.onClick.AddListener (yesEvent); // button1.onClick.AddListener (ClosePanel); // // button2.onClick.RemoveAllListeners(); // button2.onClick.AddListener (noEvent); // button2.onClick.AddListener (ClosePanel); // // this.question.text = question; // this.iconImage.sprite = iconImage; // // this.iconImage.gameObject.SetActive(true); // button1.gameObject.SetActive(true); // button2.gameObject.SetActive(true); // button3.gameObject.SetActive(false); // } // // // Yes/No/Cancel with Image: A string, a Sprite, a Yes event, a No event and Cancel event; // public void Choice (string question, Sprite iconImage, UnityAction yesEvent, UnityAction noEvent, UnityAction cancelEvent) { // modalPanelObject.SetActive (true); // // button1.onClick.RemoveAllListeners(); // button1.onClick.AddListener (yesEvent); // button1.onClick.AddListener (ClosePanel); // // button2.onClick.RemoveAllListeners(); // button2.onClick.AddListener (noEvent); // button2.onClick.AddListener (ClosePanel); // // button3.onClick.RemoveAllListeners(); // button3.onClick.AddListener (cancelEvent); // button3.onClick.AddListener (ClosePanel); // // this.question.text = question; // this.iconImage.sprite = iconImage; // // this.iconImage.gameObject.SetActive(true); // button1.gameObject.SetActive(true); // button2.gameObject.SetActive(true); // button3.gameObject.SetActive(true); // } void ClosePanel () { modalPanelObject.SetActive (false); } }
Be aware that TestModalPanel.cs is a work in progress and we adapted only a few of the functions in the scripts. The un-adapted code has been commented out. This code is still valid, and will work, but to avoid confusion, only the code used in Pt3 is uncommented.

TestModalPanel

using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; public class TestModalPanel : MonoBehaviour { public Sprite icon; public Transform spawnPoint; public GameObject thingToSpawn; public DisplayManager displayManager; private ModalPanel modalPanel; void Awake () { modalPanel = ModalPanel.Instance(); } // Send to the Modal Panel to set up the Buttons and functions to call public void TestC () { ModalPanelDetails modalPanelDetails = new ModalPanelDetails (); modalPanelDetails.question = "This is an announcement!\nIf you don't like it, shove off!"; modalPanelDetails.button1Details = new EventButtonDetails (); modalPanelDetails.button1Details.buttonTitle = "Gotcha!"; modalPanelDetails.button1Details.action = TestCancelFunction; modalPanel.NewChoice (modalPanelDetails); } public void TestCI () { ModalPanelDetails modalPanelDetails = new ModalPanelDetails {question = "This is an announcement!\nIf you don't like it, shove off!", iconImage = icon}; modalPanelDetails.button1Details = new EventButtonDetails {buttonTitle = "Gotcha!", action = TestCancelFunction}; modalPanel.NewChoice (modalPanelDetails); } // public void TestYN () { // modalPanel.Choice ("Cheese on your burger?", TestYesFunction, TestNoFunction); // } // // public void TestYNC () { // modalPanel.Choice ("Would you like a poke in the eye?\nHow about with a sharp stick?", TestYesFunction, TestNoFunction, TestCancelFunction); // } // // public void TestYNI () { // modalPanel.Choice ("Do you like this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction); // } // // public void TestYNCI () { // modalPanel.Choice ("Do you want to use this icon?", icon, TestYesFunction, TestNoFunction, TestCancelFunction); // } // // public void TestLambda () { // modalPanel.Choice ("Do you want to create this sphere?", () => { InstantiateObject(thingToSpawn); }, TestNoFunction); // } // // public void TestLambda2 () { // modalPanel.Choice ("Do you want to create two spheres?", () => { InstantiateObject(thingToSpawn, thingToSpawn); }, TestNoFunction); // } public void TestLambda3 () { ModalPanelDetails modalPanelDetails = new ModalPanelDetails {question = "Do you want to create three spheres?"}; modalPanelDetails.button1Details = new EventButtonDetails { buttonTitle = "Yes Please!", action = () => { InstantiateObject(thingToSpawn); InstantiateObject(thingToSpawn, thingToSpawn);} }; modalPanelDetails.button2Details = new EventButtonDetails { buttonTitle = "No thanks!", action = TestNoFunction }; modalPanel.NewChoice (modalPanelDetails); } // The function to call when the button is clicked void TestYesFunction () { displayManager.DisplayMessage ("Heck, yeah!"); } void TestNoFunction () { displayManager.DisplayMessage ("No way, Jose!"); } void TestCancelFunction () { displayManager.DisplayMessage ("I give up!"); } void InstantiateObject (GameObject thingToInstantiate) { displayManager.DisplayMessage ("Here you go!"); Instantiate (thingToInstantiate, spawnPoint.position, spawnPoint.rotation); } void InstantiateObject (GameObject thingToInstantiate, GameObject thingToInstantiate2) { displayManager.DisplayMessage ("Here you go!"); Instantiate (thingToInstantiate, spawnPoint.position - Vector3.one, spawnPoint.rotation); Instantiate (thingToInstantiate2, spawnPoint.position + Vector3.one, spawnPoint.rotation); } }

BringToFront

using UnityEngine; using System.Collections; public class BringToFront : MonoBehaviour { void OnEnable () { transform.SetAsLastSibling (); } }

DisplayManager

using UnityEngine; using UnityEngine.UI; using System.Collections; public class DisplayManager : MonoBehaviour { public Text displayText; public float displayTime; public float fadeTime; private IEnumerator fadeAlpha; private static DisplayManager displayManager; public static DisplayManager Instance () { if (!displayManager) { displayManager = FindObjectOfType(typeof (DisplayManager)) as DisplayManager; if (!displayManager) Debug.LogError ("There needs to be one active DisplayManager script on a GameObject in your scene."); } return displayManager; } public void DisplayMessage (string message) { displayText.text = message; SetAlpha (); } void SetAlpha () { if (fadeAlpha != null) { StopCoroutine (fadeAlpha); } fadeAlpha = FadeAlpha (); StartCoroutine (fadeAlpha); } IEnumerator FadeAlpha () { Color resetColor = displayText.color; resetColor.a = 1; displayText.color = resetColor; yield return new WaitForSeconds (displayTime); while (displayText.color.a > 0) { Color displayColor = displayText.color; displayColor.a -= Time.deltaTime / fadeTime; displayText.color = displayColor; yield return null; } yield return null; } }

6.Creating A Main Menu

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 .
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

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


LoadSceneOnClick

using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class LoadSceneOnClick : MonoBehaviour { public void LoadByIndex(int sceneIndex) { SceneManager.LoadScene (sceneIndex); } }

QuitOnClick

using UnityEngine; using System.Collections; public class QuitOnClick : MonoBehaviour { public void Quit() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit (); #endif } }

SelectOnInput

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; } }

7.Polishing Your Game Menu

In this live training session we will look at adding polish to your game menu including audio, animation and transitions using Unity's UI system.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)

This builds on content created in the Creating a Main Menu live training session which can be found here .

Download the original asset package used here

Get the audio used from the VR Samples package here

LoadOnEnter

using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class LoadOnEnter : StateMachineBehaviour { // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { SceneManager.LoadScene (1); } }

QuitOnEnter

using UnityEngine; using System.Collections; public class QuitOnEnter : StateMachineBehaviour { // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { Quit (); } public void Quit() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #else Application.Quit (); #endif } }

Recorded Video Sessions on UI
Recorded Video Sessions on UI
General Tutorial Discussion
0
0
1. The New UI
0
0
2. Creating a scene selection menu
1
2
3. Making a generic modal window
0
0
4. Making a generic modal window (Pt 2)
0
0
5. Making a generic modal window (Pt 3)
0
0
6. Creating A Main Menu
0
0
7. Polishing Your Game Menu
0
0