2015-08-20 50 views
0

我從教程中編寫了一個C#腳本,當我按空間時它工作正常。但是我想在2D手機遊戲的按鈕的情況下做到這一點。代碼如下:如何在Unity中創建加載屏幕?

using UnityEngine; 
using System.Collections; 

public class LoadingScreen : MonoBehaviour { 

public string levelToLoad; 

public GameObject background; 
public GameObject text; 
public GameObject progressBar; 

private int loadProgress = 0; 

// Use this for initialization 
void Start() { 
    background.SetActive (false); 
    text.SetActive (false); 
    progressBar.SetActive (false); 
} 

// Update is called once per frame 
void Update() { 

    if (Input.GetKeyDown ("space")) { 
     StartCoroutine(DisplayLoadingScreen(levelToLoad)); 
    } 
} 

IEnumerator DisplayLoadingScreen(string level){ 
    background.SetActive (true); 
    text.SetActive (true); 
    progressBar.SetActive (true); 

    progressBar.transform.localScale = new Vector3(loadProgress,progressBar.transform.localScale.y, progressBar.transform.localScale.z); 

    text.GetComponent<GUIText>().text = "Loading Progress " + loadProgress + "%"; 

    AsyncOperation async = Application.LoadLevelAsync (level); 
    while (!async.isDone) { 
     loadProgress = (int)(async.progress * 100); 
     text.GetComponent<GUIText>().text = "Loading Progress " + loadProgress + "%"; 
     progressBar.transform.localScale = new Vector3(async.progress,progressBar.transform.localScale.y, progressBar.transform.localScale.z); 
     yield return null; 
    } 
} 

}

我有一些技巧如何做到這一點。有人建議我做下面的代碼在按鈕的情況下:

if (GUI.Button (new Rect(),Button) { 
     StartCoroutine(DisplayLoadingScreen(levelToLoad)); 
    } 

其實我不知道該怎麼做,在按鈕的情況下。請有人幫助我在按鈕的情況下執行確切的代碼。提前致謝。

+0

通過「按鈕「,你的意思是Unity的畫布按鈕,或GUI按鈕? –

+0

其實我的意思是帆布按鈕。 @VenkatatAxiomStudios –

回答

1

在你的腳本,使一個方法:

public void onButtonClick() { 
    StartCoroutine(DisplayLoadingScreen(levelToLoad)); 
} 

然後在檢查器按鈕, 「在點擊()」 處理程序與方法相關聯:

Button Inspector

+0

明白了。謝啦。 –

相關問題