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!
使用新的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);
}
}
加载当前场景的另一种方法SceneMamager
是类似这样的:
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
确保您已包含SceneManager
在您的脚本中。
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");**
}
}
这是我的 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);
}
}