在Unity 2D中的场景之间淡入淡出

时间:2015-09-02 10:20:54

标签: c# android

我正在为Android开发游戏,我使用此代码使用按钮在两个场景之间淡入淡出:

 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);

        }
    }

附加到UI按钮的代码:

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

我收到此错误: ArgumentException:方法返回类型不兼容

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你必须写一个Coroutine才能使上述工作正常。例如

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

并在您的Button click中写如下     StartCoroutine(gameScene2());