2016-02-04 76 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