(30) unity4.6 learn Ugui Chinese Document ------- create a generic MODAL window, unity4.6ugui
Sun Guangdong
In this article, we will create a generic MODAL window (Yes, No, Maybeso, Cancel) where we can push the content and actions to the window, this window can be used anywhere in our game, and the button is pressed to work on the current event.
Code involved:
using UnityEngine;using System.Collections;public class BringToFront : MonoBehaviour {void OnEnable () {transform.SetAsLastSibling ();}}
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 eventpublic 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);}}
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;}}
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 callpublic 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 UnityActionsvoid TestYesFunction () {displayManager.DisplayMessage ("Heck yeah! Yup!");}void TestNoFunction () {displayManager.DisplayMessage ("No way, José!");}void TestCancelFunction () {displayManager.DisplayMessage ("I give up!");}}
Other things:
Resolution & Device Independence
PlayerSettings:
IPhone 6 Plus: X resolution
Resolution in a ratio can be enabled through the definition of mouding, such.
Obviously, the elements in the four corners can be directly placed on the corresponding feet!
The center is in the center.
The upper, lower, and left are the corresponding upper, lower, and right sides.
However, when the screen is changed to an hour, the following problems still occur:
The Canvas Scaler component is coming soon.
In this way, when the screen size changes, the UI is no longer simply fixed and will become smaller as it grows.
After the Creating a Scene Selection Menu scenario is changed, you need to switch the sound: void OnLevelWasLoaded (int level) {if (level = 2) {source. clip = level2Music; source. play () ;}} asynchronous loading scenario: 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) {l OadingImage. SetActive (true); StartCoroutine (LoadLevelWithBar (level);} IEnumerator LoadLevelWithBar (int level) {async = Application. LoadLevelAsync (level); while (! Async. isDone) {loadingBar. value = async. progress; yield return null ;}}}