Unity scene switching + exit program method collection detailed version

The detailed version is suitable for novices. The scripts are copied from other articles. Now I can write it myself. This blog will be used as a souvenir and will not be deleted.

(1) Progress bar switching scene

Renderings:

①Create two new scenes and rename them to Scene0 and Scene1 respectively. (Add scene: right mouse button→Create→Scene)

②Create a new Canvas, create a Quad and drag the desired background image, create Test (for displaying progress), Iamge (for displaying progress bar), adjust the size and position.

Drag a white picture (other pictures are also available) to Unity, and then set the image.

Change the Texture Type of the image to Sprite (2D and UI), then drag the image onto the image

Continue to set as shown in the figure

Image Type (picture display type) to Filled (filled)

Fill Method (fill method) to Horizontal (horizontal fill)

Fill Amount (fill ratio): 0 is not displayed at all, 1 is fully displayed.

②Create a new script ( Visual Studio ) and rename it to LoadScene (right mouse button→Create→C#Script)

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadScene : MonoBehaviour
{
    public Text loadingText;
    public Image progressBar;
    private int curProgressValue = 0;

    void FixedUpdate()
    {
        int progressValue = 100;

        if (curProgressValue < progressValue)
        {
            curProgressValue++;
        }

        loadingText.text = $"正在努力加载游戏资源...{curProgressValue}%";//实时更新进度百分比的文本显示  

        progressBar.fillAmount = curProgressValue / 100f;//实时更新滑动进度图片的fillAmount值  

        if (curProgressValue == 100)
        {
            loadingText.text = "OK";//文本显示完成OK
            SceneManager.LoadScene("Scene1");
        }
    }
}

Drag the script LoadScene to the main camera of the camera, and assign values ​​to --- in the script as shown in the figure

Adjust camera position.

(2) Button to switch scenes

①Create a button in the scene and create a new scene Scene2

②Create a new script and rename it to ChangeScene

using UnityEngine;
using System.Collections;
using UnityEngine.UI;//注意这个不能少
public class ChangeScene : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {

        GameObject btnObj = GameObject.Find("开始游戏");//"开始游戏"为你的Button的名称
        Button btn = btnObj.GetComponent<Button>();
        btn.onClick.AddListener(delegate ()
        {
            this.GoNextScene(btnObj);
        });
    }

    // Update is called once per frame
    void Update()
    {
    }

    public void GoNextScene(GameObject NScene)
    {
        Application.LoadLevel("Scene2");//切换到场景Scene2
    }
}

Line 23 Application.LoadLevel("Scene_2"); //Switching to scene Scene2 may be outdated. To modify it, change it to SceneManager.LoadScene("Scene_2"); //Switch to scene Scene2; the reference should also be added. Add using UnityEngine.SceneManagement after line 3 ; ] Use it if it works

 //The eighth line GameObject btnObj = GameObject.Find(" Button "); Button needs to be changed to your Button name

//The twenty-third line Application.LoadLevel(" Scene2 "); Scene2 needs to be changed to your scene name

renderings

(3) Simple version of button switching scene (the effect is the same as 2)

Create a button in the scene and hang the script Choose on the button

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

public class Choose : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        this.GetComponent<Button>().onClick.AddListener(OnClick);
    }

    void OnClick()
    {
        SceneManager.LoadScene("Scene");//Scene为我们要切换到的场景
    }

    // Update is called once per frame
    void Update()
    {

    }
}

Just change the Scene in the eighteenth line of the script SceneManager.LoadScene(" Scene "); to your button name

(4) Button to switch scenes

① Add a button and create a new script StartScript.

using UnityEngine;
using System.Collections;

public class StartScript : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    //之所以要如此复杂写一个函数接口,是因为UGUI事件函数需要。
    //理论上是可以直接写 public void OnStartGame(){Application.LoadLevel("Game");}
    //但这样一、不规范,二、脚本的复用性大大降低,如果这里有N个地方要实现场景撤换,那么则会有N个脚本
    //所以要这样写,我也不想弄这样的函数接口!-_-!
    public void OnStartGame(string sceneName)
    {
        Application.LoadLevel(sceneName);
    }
}

[ Line 23 Application.LoadLevel(sceneName); may be outdated. To modify it, change to SceneManager.LoadScene(sceneName);  the reference should also be added. Add using UnityEngine.SceneManagement after line 3 ; ] Use it if it works

②Create an empty object GameObject (right mouse button on the Hierarchy panel → Create Empty)

Drag the script to the empty object and set the button event as shown in the figure

☆ High reusability, multiple buttons only need to set button events, no need to write several scripts

renderings

(5) Exit program button

① Add a button, then add a script CloseScript

using UnityEngine;
using System.Collections;

public class CloseScript : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    //这里还是无参数的,因为关闭游戏本身就不需要任何参数,好嘛-_-!
    public void OnCloseGame()
    {
        Application.Quit();
    }
}

② Assign the script to an empty object GameObject

③Set the button event as shown in the figure

Implement the exit game function in Unity

We want to exit the editor in the editor environment and exit the game in a non-compiler environment

ask questions

answer

Use preprocessing to determine the current environment

Commonly used preprocessing identifiers

identifier explain
UNITY_EDITOR Compile only in the editor
UNITY_ANDROID Compile only on Android
UNITY_IPHONE Compile only under Apple system
UNITY_STANDALONE_OSX Definitions specifically for the Mac OS (including Universal, PPC, and Intelarchitectures) platforms
UNITY_STANDALONE_WIN Compile only under Windows system


 

Set the isPlaying property of EditorApplication to false to exit the editor mode

Call Application's Quit() method to exit the program

the code

public void ExitGame()
    {
        //预处理
#if UNITY_EDITOR    //在编辑器模式下
        EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }

Guess you like

Origin blog.csdn.net/weixin_57362299/article/details/117906120