2015-09-02 108 views
1

場景之間的衰落,我開發Android遊戲,我用這個代碼用一個按鈕兩個場景之間褪色:在Unity 2D

:連接到UI按鈕

public class fading : MonoBehaviour { 

     public Texture2D fadeOutTexture; 
     public float fadeSpeed = 0.8f; 

     private int drawDepth = -1000; 
     private float alpha = 1.0f; 
     private int fadeDir = -1; 


     void onGUI() { 

      alpha += fadeDir * fadeSpeed * Time.deltaTime; 
      alpha = Mathf.Clamp01(alpha); 
      GUI.color = new Color (GUI.color.g, GUI.color.b, alpha); 
      GUI.depth = drawDepth; 
      GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeOutTexture); } 

     public float BeginFade (int direction) { 
      fadeDir = direction; 
      return (fadeSpeed); } 

     void onLevelWasLoaded() { 
      BeginFade (-1); 

     } 
    } 

代碼

public void gameScene2() { 
     float fadeTime = GameObject.Find ("scene2Choose").GetComponent<fading>().BeginFade(1); 
     yield return new WaitForSeconds(fadeTime); 
     Application.LoadLevel("gameScene2"); 
    } 
} 

我得到這個錯誤: 的ArgumentException:方法的返回類型是不兼容的

我在做什麼錯?

回答

2

你必須寫一個Coroutine爲了上面的工作。對於e.g

IEnumerator gameScene2() { 
    float fadeTime = GameObject.Find ("scene2Choose").GetComponent<fading>().BeginFade(1); 
    yield return new WaitForSeconds(fadeTime); 
    Application.LoadLevel("gameScene2"); 
} 

,並在您Button click寫像下面 StartCoroutine(gameScene2());

+0

我已經添加上面的代碼,謝謝。當你說按鈕點擊時,你的意思是檢查器中的點擊()?我看不到一個StartCouroutine選項。我讀過,我需要將StartCorotine放在Update方法中,所以如何將我使用的按鈕鏈接到此? – JGrn84

+0

你可以試試這個:http://answers.unity3d.com/questions/777818/46-ui-calling-function-on-button-click-via-script.html –