Unity 5.3 How to load current level?

只谈情不闲聊 提交于 2019-11-27 06:37:12

问题


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!


回答1:


Use the new SceneManager and make sure you include the namespace 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);
    }
}



回答2:


Another way for loading current scene with SceneMamager is Something Like this :

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

be sure that you have include SceneManager in your script.




回答3:


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



回答4:


it's my C# example:) i had the same problem and now i have figured out with it, you must keep in mind that your scene must be included to the build settings of your project;) hope this will help others with new canges in it) Cheers:)
P.S. Add this script to your button in inspector and choose you r script and name of this function:)

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

}


来源:https://stackoverflow.com/questions/34170650/unity-5-3-how-to-load-current-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!