Unity level jump - the realization of switching from the start interface to the game scene

1. On the basis of the existing level, create another level, name it start and save it.

2. Create the TitleScreen.cs script in the newly created start level, the code is as follows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TitleScreen : MonoBehaviour
{
    //响应游戏开始事按钮件
    public void OnButtonGameStart()
    {
        SceneManager.LoadScene("level1");  //读取关卡level1
    }
}

(It’s worth noting that don’t forget to use the SceneManagement class provided by the unity engine, that is, add using UnityEngine.SceneManagement at the top of the script;)

3. Mount the TitleScreen.cs script on the camera.

4. Create a UI Canvas, and then select [UI]->[Image] to create an image UI, named Image_background here. Specify a texture in Source Image as the background (the default .png format is Texture type, which cannot be directly used on the UI). Here we first convert the sample image to sprite type.

f39b2696095248f6beb287380e0890c8.jpeg2cb15fd0a8e44e41afa6fa8e880531a4.jpeg

 5. Add title text, and finally create a button [UI]->[Button], and modify the text content to "Game Start".

4e0171c03c19471da441ea58536bca30.jpeg

9027d461128347c2be5d8490c91a4c15.jpeg

 6. Select Button_gamestart, then click the "+" button under On Click (), specify the camera as the message receiving object, and select the OnButtonGameStart function of TitileScreen as the callback function that responds to the button click event. 

 316fc2a9e82e42a8ae9051f6947f7b13.jpeg

7. Select [File]->[Build Settings] in the editor menu bar to add levels.8caca6185975499f85e05f6bd092930c.jpeg

 Run the game, now you can jump to the "level1" level by clicking the "Start Game" button.

 

Guess you like

Origin blog.csdn.net/m0_64688993/article/details/127272115