How to reload the current scene in Unity?
You can use the LoadScene method from the SceneManager class in Unity to reload the current scene. Here is an example code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class ReloadScene : MonoBehaviour
{
public void Reload()
{
// 获取当前场景的名称
string sceneName = SceneManager.GetActiveScene().name;
// 重新加载当前场景
SceneManager.LoadScene(sceneName);
}
}
In the above example, we created a script called ReloadScene and defined a public method called Reload within it. When we want to reload the scene, we just need to call the Reload method.
Before using this method, make sure you have added the SceneManager namespace to your script as shown below:
using UnityEngine;
using UnityEngine.SceneManagement;
Furthermore, you need to attach the ReloadScene script to a GameObject, such as a button object. Next, bind the OnClick event to the Reload method, so that when the button is clicked, the current scene will be reloaded.