(三十)unity4.6學習Ugui中文文檔-------製作一個泛型的MODAL視窗,unity4.6ugui

來源:互聯網
上載者:User

(三十)unity4.6學習Ugui中文文檔-------製作一個泛型的MODAL視窗,unity4.6ugui

孫廣東 2015.5.11

在此文章中我們將製作一個泛型的MODAL視窗 (Yes, No, Maybeso, Cancel) 在那裡我們可以把內容和動作push到視窗中,這個視窗可以在我們的遊戲的任何地方使用,按鈕被按下時事件工作。


涉及到的代碼:

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!");}}


說說別的:

 Resolution & Device Independence

PlayerSettings :

iPhone6 Plus:具備1920x1080解析度

在一個比例下的解析度通過 牟定的概念可以 開啟適配,如16:9

明顯在四個角落上的元素直接牟定在對應的腳上即可! 

正中間就定在正中間。

上下左右就定在對應的上下左右。

但是螢幕變小時依然會出現很大的問題:

 

接下來要登場的是:Canvas Scaler 組件

這樣,當螢幕大小發生變化後,UI不再是簡單的牟定了,會隨著變大變小。


Creating a Scene Selection Menu情境更改後要切換聲音:MonoBehaviour中的 void OnLevelWasLoaded(int level)    {        if (level == 2)        {            source.clip = level2Music;            source.Play ();        }    }非同步載入情境: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;        }    }}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.