1. First create the first scene and create a Button in Canvas
The shortcut key is Ctrl+N, and then press Ctrl+S to save the scene to a file
As shown in the picture -------------------------------------------- ------------------------------------------------ The start is button
2. Create a second scene as the scene to switch after clicking the button
Click File-Build Settings in the upper left corner
Drag the second scene into the Scenes In Bulid box
This step must be done! ! ! Otherwise the executed code cannot recognize the scene
3. Create an empty object GameObject in the first scene Canvas, and add code
Click on the empty object GameObject to create a Move To Scene script, Edit Script to edit the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;/*切换场景必须添加的前缀*/
public class MoveToScene : MonoBehaviour
{
void Start()
{
}
void Update()
{
}
public void ChangeScene()/*定义一个切换场景*/
{
SceneManager.LoadScene("你要切换的场景名");/*双引号里面填入你创建的场景命名*/
}
}
*Note: Be sure to add using UnityEngine.SceneManagement; prefix --------------------------
4. Move the empty object GameObject to On Click() in the Button component of the button
In the UI that has been created in the first scene: In the Inspector of Button, find that there is a button at the bottom of the Button component
On Click() is empty by default, so we first click the "+" in the lower right corner to create
Move the GameObject into None(Object)
Click No Function, select the script we have written Move To Scene