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

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 框中,如下:

         运行效果如下:

  • 32
    点赞
  • 84
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

little_fat_sheep

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值