2016-02-04 77 views
1

我使用Unity 5.3.1。 我想要使用淡入淡出效果改变场景。LoadScene与衰落C#

一切工作,但是,我的新的场景为 “灰色” ......像这样:

为灰色图像 enter image description here

相反的:

定期图像 enter image description here

我的衰落:

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.r,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) 
{ 
    Debug.Log("BeginFade"); 
    fadeDir = direction; 
    return fadeSpeed; 
} 

void OnLevelWasLoaded(int level) 
{ 
    Debug.Log("wasLoaded"); 
    BeginFade(-1); 
} 

}

而且mycode的谁执行我的场景:

IEnumerator ChangeLevel() 
{ 
    float fadeTime = GameObject.Find("GM").GetComponent<Fading>().BeginFade(1); 
    yield return new WaitForSeconds(fadeTime); 
    SceneManager.LoadScene(scene); 
} 

而且我ChangeLevel函数的调用:

if (other.gameObject.name == "MenuController") 
{ 
    StartCoroutine(ChangeLevel()); 
} 

回答

0

是您的游戏对象设置为DontDestroyOnLoad? 另外我认为你的代码中有一个逻辑错误。 您使用BeginFade提供的时间调用WaitForSeconds。 这个时间是fadeSpeed,这与您在屏幕变黑之前等待的时间并不相同。 目前您的alpha += fadeDir * fadeSpeed * Time.deltaTime;表示每秒钟都会给Alpha添加一个0.8的值。但是,如果Alpha设置为1,Alpha只会完全透明。

所以实际的时间应该是1/fadeSpeed,我认为(1.25s)。 为什么屏幕保持灰色我不能告诉你。它应该下降,除非它为0.

现在,在整个时间启用一个带有OnGUI的组件并不是一个好主意。即使它没有显示出来,MonoBehavior有一个OnGUI方法的事实会产生很大的开销。自Unity5发布以来,有很多更好的方法来进行屏幕截图。只是谷歌一点点,你会发现项目的像theese(未测试):

+0

谢谢!我会找到它! –

1

我想这是因为在Unity 5.发生了很多照明的bug当游戏级别重新加载时。

转到Unity>Window>Lighting,取消选中对话框底部的auto选项,然后再次测试您的游戏。

以下是说明:How to disable auto light baking in Unity