zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【Unity3D】场景切换、全屏/恢复切换、退出游戏、截屏

游戏 场景 恢复 切换 Unity3D 退出 全屏 截屏
2023-09-11 14:20:52 时间

1 前言

        1)场景切换

        场景切换可以使用 SceneManager 的 LoadScene 和 LoadSceneAsync 方法,如下:

public static void LoadScene(string sceneName)
public static void LoadScene(int sceneBuildIndex)
public static AsyncOperation LoadSceneAsync(string sceneName)
public static AsyncOperation LoadSceneAsync(int sceneBuildIndex)

        切换场景会默认销毁当前场景中的所有游戏对象,若不想销毁某对象,可以调用 MonoBehaviour 的 DontDestroyOnLoad 方法,如下:

DontDestroyOnLoad(gameObject);

        2)全屏 / 恢复切换

         全屏 / 恢复切换,可以使用 Screen.SetResolution 接口,如下:

        WindowController.cs

using System.Runtime.InteropServices;
using UnityEngine;

public class WindowController : MonoBehaviour {
    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetSystemMetrics(int nIndex);
    private const int SM_CXSCREEN = 0; // 主屏幕分辨率宽度
    private const int SM_CYSCREEN = 1; // 主屏幕分辨率高度
    private const int SM_CYCAPTION = 4; // 标题栏高度
    private const int SM_CXFULLSCREEN = 16; // 最大化窗口宽度(减去任务栏)
    private const int SM_CYFULLSCREEN = 17; // 最大化窗口高度(减去任务栏)
    private const float INIT_WINDOW_RATE = 0.8f; // 初始窗口比例
    private int screenWidth; // 屏幕宽度
    private int screenHeight; // 屏幕高度
    private int windowWidth; // 窗口宽度
    private int windowHeight; // 窗口高度

    private void Awake() {
        screenWidth = GetSystemMetrics(SM_CXSCREEN);
        screenHeight = GetSystemMetrics(SM_CYSCREEN);
        windowWidth = (int) (screenWidth * INIT_WINDOW_RATE);
        windowHeight = (int)(screenHeight * INIT_WINDOW_RATE);
    }

    private void Update() {
        if (Input.GetKeyDown(KeyCode.Escape)) {
            if (!Screen.fullScreen) {
                windowWidth = Screen.width;
                windowHeight = Screen.height;
                Screen.SetResolution(screenWidth, screenHeight, true);
            } else {
                Screen.SetResolution(windowWidth, windowHeight, false);
            }
        }
    }
}

         说明:在打包应用时,需要在 Player Settings 窗口勾选 Resizable Window 选项,如下:

        3)退出游戏

         退出游戏可以使用 Application 的 Quit 方法,支持后台运行(默认不支持)可以使用 Application 的 runInBackground 属性,如下:

if (Input.GetKeyDown(KeyCode.Q)) {
    Application.Quit(); //退出游戏
}
// 设置支持后台运行
Application.runInBackground = true;

        4)截屏

ScreenCapture.CaptureScreenshot("Assets/Screenshot.png");

2 应用

2.1 场景一

        1)游戏对象

        2)游戏界面

        3)脚本组件

        SceneController1.cs

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SceneController1 : MonoBehaviour {
	private bool fullScreen = true;

	private void Start () {
		Button jumpBtn = transform.GetComponent<Button>();
		jumpBtn.onClick.AddListener(OnClickJump);
	}

	private void Update() {
		if (Input.GetKeyDown(KeyCode.Escape)) { // 全屏与恢复
			fullScreen = !fullScreen;
			Screen.fullScreen = fullScreen;
		}
		if (Input.GetKeyDown(KeyCode.Q)) { // 退出游戏
			Application.Quit();
		}
	}

	private void OnClickJump() { // 切换场景
		SceneManager.LoadScene("Scene2");
		// SceneManager.LoadSceneAsync("Scene2");
	}
}

        说明: SceneController1 脚本组件挂在 Button 控件上。

2.2 场景二

        1)游戏对象

        2)游戏界面

        3)脚本组件

        SceneController2.cs

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SceneController2 : MonoBehaviour {
	private bool fullScreen  = true;

	private void Start () {
		Button jumpBtn = transform.GetComponent<Button>();
		jumpBtn.onClick.AddListener(OnClickJump);
	}

	private void Update() {
		if (Input.GetKeyDown(KeyCode.Escape)) { // 全屏与恢复
			fullScreen = !fullScreen;
			Screen.fullScreen = fullScreen;
		}
		if (Input.GetKeyDown(KeyCode.Q)) { // 退出游戏
			Application.Quit();
		}
	}

	private void OnClickJump() { // 切换场景
		SceneManager.LoadScene("Scene1");
		// SceneManager.LoadSceneAsync("Scene1");
	}
}

        说明: SceneController2 脚本组件挂在 Button 控件上。

2.3 场景切换

        依次选择【File→Build Settings】(或按 Ctrl + Shift + B 快捷键),将 Scene1 和 Scene2 拖拽到 Scenes In Build 框中,如下:

         运行效果如下: