How to achieve changing scenes by click in Unity 2D?show me code
时间: 2023-11-22 08:55:31 浏览: 77
To achieve changing scenes by click in Unity 2D, you can follow these steps:
1. Create a new scene in Unity and add a button to the scene.
2. Attach a script to the button to handle the click event.
3. In the script, use the SceneManager.LoadScene() method to load the new scene.
Here is an example code snippet:
```csharp
using UnityEngine.SceneManagement;
using UnityEngine;
public class SceneChanger : MonoBehaviour
{
public void ChangeScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
}
```
In this script, we are using the ChangeScene() method to load a new scene when the button is clicked. You can attach this script to the button and set the scene name in the button's OnClick event.
阅读全文