在Unity 2D的场景之间褪色

本文关键字:之间 褪色 Unity 2D | 更新日期: 2023-09-27 18:10:46

我正在开发一款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:方法返回类型不兼容

我做错了什么?

在Unity 2D的场景之间褪色

你必须写一个Coroutine为了上面的工作。例如

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

和在你的Button click写如下StartCoroutine(gameScene2());