17

before Unity 5.3, I could do

Application.LoadLevel(Application.loadedLevel);

But now it's something weird with SceneManager. I've read documentation but nothing. How do I get the current scene and load it (Unity 5.3f4)?

Thanks!

4

4 回答 4

23

使用新的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);
    }
}
于 2015-12-09T09:10:19.420 回答
3

加载当前场景的另一种方法SceneMamager是类似这样的:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);

确保您已包含SceneManager在您的脚本中。

于 2018-12-09T18:43:35.350 回答
-1
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");**  
    }   
}
于 2016-08-19T10:38:09.720 回答
-3

这是我的 C# 示例:) 我遇到了同样的问题,现在我已经解决了,您必须记住,您的场景必须包含在项目的构建设置中;) 希望这将有助于其他人在其中遇到新问题) 干杯:)
PS将此脚本添加到检查器中的按钮并选择您的脚本和此函数的名称:)

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

}
于 2016-02-23T18:37:41.463 回答