当前位置:网站首页>Unity scene jump and exit

Unity scene jump and exit

2022-07-06 12:33:00 SQ Liu

brief introduction :

Unity Realize simple jump of each scene in , And click “ Exit scene ” Close the interface .

Realization effect :

 Insert picture description here
 Insert picture description here

Implementation steps :

One 、 New scene
In the project you created , Add several scenarios that need to jump , Here we use 4 Take the jump between scenes as an example .4 The scene names are shown in the following figure .
 Insert picture description here
In the scene above , You can first create a corresponding scene name 3D object , for example , scene Capsule in , You can create several capsules Capsule( As shown in the figure below ); scene Cube in , You can create several cubes Cube; scene Sphere in , You can create several spheres Sphere. What specific scenarios are created , According to your respective projects , And the scene that needs to complete the jump , Here is just a case .
 Insert picture description here
 Insert picture description here
Two 、 Create jump button
1、 As shown in the figure below , establish UI-Button.
 Insert picture description here
2、 Modify canvas Canvas Of UI Scale Mode And its resolution .
 Insert picture description here
 Insert picture description here

3、 modify Button name , See the name and know the meaning , Prevent confusion caused by adding other buttons later , The specific operation is shown in the figure below .
 Insert picture description here
4、Ctrl+D Copy 4 Share , Rename their buttons to BtnCube,BtnMain,BtnSphere,BtnTuiChu, And adjust their position in the canvas , As shown in the figure below .
 Insert picture description here
3、 ... and 、 Add code
1、 Create a new one called “Scripts” Folder , Used to store all C# Script .
 Insert picture description here
2、 All jump codes are as follows :

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

public class ChangJingTiaoZhuan : MonoBehaviour
{
    
    public void TiaoZhuanCapsule()
    {
    
        SceneManager.LoadScene("Capsule");
    }
    public void TiaoZhuanCube()
    {
    
        SceneManager.LoadScene("Cube");
    }
    public void TiaoZhuanMain()
    {
    
        SceneManager.LoadScene("Main");
    }
    public void TiaoZhuanSphere()
    {
    
        SceneManager.LoadScene("Sphere");
    }
    public void TuiChuChangJing()
    {
    
        //#if UNITY_EDITOR
        //UnityEditor.EditorApplication.isPlaying = false;// Exit in editing state 
        //#else
        Application.Quit();// Package and compile and exit 
        //#endif
    }
}

Four 、 Add button event registration
1、 Drag the script to Canvas in , As shown in the figure below .
 Insert picture description here
2、 The specific operation is shown in the figure below .
 Insert picture description here
 Insert picture description here
3、 The reason for adding the following buttons to respond to events is the same as above , Be careful not to add dislocation to the corresponding event .

4、 When all are added , Save the current scene .
5、 Mouse selected Canvas and EventSystem, Right click and select Copy Copy, As shown in the figure below .
 Insert picture description here

6、 Double click the mouse to enter the scene Cube, Then paste Paste In the previous scene Canvas.
 Insert picture description here
7、 additional Main Scene and Sphere The scene operation steps are the same .
8、 Click on File, choice Build Settings, Drag in all the scenes that need to jump , As shown in the figure below .
notes : This step must have , Otherwise, the scene to jump will not be found after running !!
 Insert picture description here
 Insert picture description here
9、 thus , Jump switching between different scenes , And the exit scenario operation is completed .
5、 ... and 、 Engineering release
1、 Click on File, choice Build Settings, choice Player Settings, Modify some of its properties , Click when finished “Build” that will do .
 Insert picture description here
2、Build After completion , Open its folder , Double-click to open “TiaoZhuan.exe” The program is ready to run .
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[SQ Liu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913596614.html