当前位置:网站首页>【Unity3D】场景切换、退出全屏、退出游戏
【Unity3D】场景切换、退出全屏、退出游戏
2022-07-29 11:31:00 【little_fat_sheep】
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)
2)退出全屏
退出全屏可以使用 Screen 的 fullScreen 属性,如下:
if (Input.GetKeyDown(KeyCode.Escape)) {
fullScreen = !fullScreen;
Screen.fullScreen = fullScreen;
}
3)退出游戏
退出游戏可以使用 Application 的 Quit 方法,如下:
if (Input.GetKeyDown(KeyCode.Q)) {
Application.Quit();
}
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 框中,如下:
运行效果如下:
边栏推荐
- 大伟 Golang之路
- MySql启动、连接sqlog、主从复制、双机热备(开机时)
- Peking University open classes are coming! Welcome to the "AI for science" class
- Applied practical skills of deep reinforcement learning
- The heavyweight foundation awarded platinum, gold and silver donors
- Summer vacation training week1
- 微信怎么知道别人删除了你?批量检测方法(建群)
- 什么是 Kubernetes 自定义资源定义 (CRD)?
- Niuke net brush questions
- 3.认识和操作一下mysql的基本命令
猜你喜欢
8. Interleave - understand ThreadPoolExecutor thread pool from architecture design to practice
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
MySql启动、连接sqlog、主从复制、双机热备(开机时)
Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
【无标题】
【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
PHP basics uses arrays to save data
How to start writing helm charts for your kubernetes application
自采集在线电脑壁纸php源码v2.0自适应端
随机推荐
QML(二):设置自定义窗体
考完PMP后有什么益处
迁徙数据平台简单介绍
大伟 GBase8s游标稳定性读ESQL测试用例
[image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
「PHP基础知识」使用数组保存数据
2022最新 wifi大师小程序独立版3.0.8
3.认识和操作一下mysql的基本命令
『知识集锦』一文搞懂mysql索引!!(建议收藏)
MyCat中间件高可用、读写分离、分片、主从切换、ER分片
SkiaSharp of WPF custom painting to bounce ball (case)
Spark efficient data analysis 01. Establishment of idea development environment
Learn weekly - 64 - a v2ex style source BBS program
Flink UDF 函数汇总
Starrocks technology insider: how to have both real-time update and fast query
报表控件FastReport与StimulSoft功能对比
Talk about the establishment of performance testing environment
微信发红包测试用例
QT's user-defined interface (borderless and movable)
️ 炒 股 实 战丨原 地 起 飞 ️