2015-12-09 23 views
14

,我可以做Unity 5.3如何加載當前級別?統一5.3之前

Application.LoadLevel(Application.loadedLevel); 

但現在的一些奇怪的與場景管理。我讀過文檔,但沒有。我如何獲取當前場景並加載它(Unity 5.3f4)?

謝謝!

+0

或'SceneManager.LoadScene(SceneManager.GetActiveScene()buildIndex)的腳本和名稱;' – tim

回答

20

使用新SceneManager並確保包括命名空間UnityEngine.SceneManagement

using UnityEngine.SceneManagement; 

public class Example 
{ 
    public void ReloadCurrentScene() 
    { 
     // get the current scene name 
     string sceneName = SceneManager.GetActiveScene().name; 

     // load the same scene 
     SceneManager.LoadScene(sceneName,LoadSceneMode.Single); 
    } 
} 
-3

這是我的C#示例:)我有同樣的問題,現在我已經想通了,有了它,你必須記住,你的場景必須包含到您的項目的構建設置;) 希望這將幫助其他新的裝置在其中) 乾杯:)
PS這個腳本添加到您的按鈕,檢查,並選擇您R此功能:)

using UnityEngine; 
using System.Collections; 
using UnityEngine.SceneManagement; 

public class start_new_game : MonoBehaviour { 

// Use this for initialization 
void Start() { 

} 

// Update is called once per frame 
public void Update() { 

} 
public void OnMouseDown() 
{ 
     SceneManager.LoadScene(0); 
} 

} 
+0

這是怎麼回事如果通過場景管理器加載,所有先前加載的對象將保持不變 – GameDeveloper

0
using UnityEngine;  
using UnityEngine.UI; 
using System; 
using System.Collections; 
**using UnityEngine.SceneManagement;** 

public class UIManager : MonoBehaviour{ 

public void OnRoomJoin(BaseEvent e) 
    { 

     // Remove SFS2X listners and re-enable interface before moving to the main game scene 
     //Reset(); 

     // Goto the main game scene 
     **SceneManager.LoadScene(1);** 
//  **SceneManager.LoadScene("main");** 
    } 
}