(三十)unity4.6学习Ugui中文文档-------制作一个泛型的MODAL窗口
2015-05-12 07:47
253 查看
孙广东 2015.5.11
在此文章中我们将制作一个泛型的MODAL窗口 (Yes, No, Maybeso, Cancel) 在那里我们可以把内容和动作push到窗口中,这个窗口可以在我们的游戏的任何地方使用,按钮被按下时事件工作。
涉及到的代码:using UnityEngine;
using System.Collections;
public class BringToFront : MonoBehaviour {
void OnEnable () {
transform.SetAsLastSibling ();
}
}
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;
}
}
说说别的: Resolution & Device IndependencePlayerSettings :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;
}
}
}
在此文章中我们将制作一个泛型的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 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); } }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 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!"); } }
说说别的: Resolution & Device IndependencePlayerSettings :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;
}
}
}
相关文章推荐
- (二十七)unity4.6学习Ugui中文文档-------Unity3D UI (uGUI)窗口扩展
- ASP.NET制作一个简单的等待窗口
- ChatGirl 一个基于 TensorFlow Seq2Seq 模型的聊天机器人[中文文档]
- Asp.net制作一个简单的等待窗口
- 推荐一个android中文开发文档网址
- html中 target 的所有 4 个值都以下划线开始。任何其他用一个下划线作为开头的窗口或者目标都会被浏览器忽略,因此,不要将下划线作为文档中定义的任何框架 name 或 id 的第一个字符。
- (十五)unity4.6学习Ugui中文文档-------技巧-Designing UI for Multiple Resolutions
- 打开多个Excel2010文档在同一个窗口中显示问题如何解决
- 如何让word文档都在同一个窗口下打开 (反之亦可)
- 一个LaTeX 中文文档的简单而实用的模板
- Asp.net制作一个简单的等待窗口
- 如何禁用窗口关闭按钮在一个 MFC 多文档界面 (MDI) 应用程序
- (九)unity4.6学习Ugui中文文档-------参考-UGUI Rect Transform
- 怎样简单的制作一个CHM格式的帮助文档?
- 一个完整的利用xeCJK包书写中文文档的例子
- 【D3中文api文档制作小组英雄帖】
- Joomla实现组件中弹出一个模式(modal)窗口的方法
- (十四)unity4.6学习Ugui中文文档-------参考-UGUI Auto Layout
- Linux From Scratch 中文制作文档
- 创建一个新的视图窗口来显示已打开的文档中的内容