Unity 5.3 如何加载当前关卡?

17

在Unity 5.3之前,我可以做

Application.LoadLevel(Application.loadedLevel);

但现在SceneManager的情况有些奇怪。我已经阅读了文档,但没有找到答案。我该如何获取当前场景并加载它(Unity 5.3f4)?

谢谢!


1
或者 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); - tim
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);
    }
}

3

使用 SceneManager 加载当前场景的另一种方式如下:

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

请确保在您的脚本中已经包括了SceneManager


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

-3

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

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

}

这将会有bug,如果你通过场景管理器加载,所有先前加载的对象都将保留。 - CoffeDeveloper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接