当前位置:网站首页>Unity场景跳转及退出
Unity场景跳转及退出
2022-07-06 09:18:00 【SQ刘】
简介:
Unity中实现简单的各个场景跳转,以及实现发布后点击“退出场景”实现界面的关闭操作。
实现效果:


实现步骤:
一、新建场景
在你创建的工程里,添加几个需要实现跳转的场景,这里以4个场景相互跳转为例。4个场景名如下图所示。
上图场景中,可以先创建对应场景名的3D物体,例如,场景Capsule中,就可以创建几个胶囊体Capsule(如下图所示);场景Cube中,就可以创建几个立方体Cube;场景Sphere中,就可以创建几个球体Sphere。具体创建什么场景,根据大家各自项目,以及需要完成跳转的场景来定,我这里只是案例说明。

二、创建跳转按钮
1、如下图所示,创建UI-Button。
2、修改画布Canvas的UI Scale Mode及其分辨率。

3、修改Button名,做到见名知意,防止后面添加其他按钮造成混淆,具体操作如下图所示。
4、Ctrl+D复制4份,分别将其按钮重命名为BtnCube,BtnMain,BtnSphere,BtnTuiChu,并调整它们在画布中的位置,如下图所示。
三、添加代码
1、新建一个名为“Scripts”的文件夹,用来存放所有的C#脚本。
2、全部跳转代码如下:
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;//编辑状态下退出
//#else
Application.Quit();//打包编译后退出
//#endif
}
}
四、添加按钮事件注册
1、将脚本拖到Canvas里,如下图所示。
2、具体操作如下图所示。

3、下面几个按钮响应事件的添加道理同上,注意对应事件不要添加错位即可。
4、全部添加完成后,保存当前场景。
5、鼠标选中Canvas和EventSystem,右击选择复制Copy,如下图所示。
6、鼠标双击进入场景Cube,然后粘贴Paste上一个场景中的Canvas。
7、另外的Main场景和Sphere场景操作步骤同上。
8、点击File,选择Build Settings,将需要实现跳转的场景全部拖进来,如下图所示。
注:这一步一定要有,不然运行后会找不到要跳转的场景!!

9、至此,各个不同场景之间的跳转切换,以及退出场景操作全部完成。
五、工程发布
1、点击File,选择Build Settings,选择Player Settings,修改其部分属性,完成后点击“Build”即可。
2、Build完成后,打开其所在文件夹,双击打开“TiaoZhuan.exe”程序即可运行。


边栏推荐
猜你喜欢

Redis based distributed locks and ultra detailed improvement ideas

JS variable types and common type conversions

Understanding of AMBA, AHB, APB and Axi

NRF24L01故障排查

Kconfig Kbuild

Expected value (EV)

MySQL時間、時區、自動填充0的問題

Navigator object (determine browser type)

Arduino uno R3 register writing method (1) -- pin level state change

First use of dosbox
随机推荐
SSD technical features
2021.11.10 compilation examination
HCIP Day 12
基於Redis的分布式ID生成器
Arm pc=pc+8 is the most understandable explanation
[offer9]用两个栈实现队列
Pytoch implements simple linear regression demo
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
【ESP32学习-1】Arduino ESP32开发环境搭建
Mysqldump error1066 error solution
Basic operations of databases and tables ----- creating data tables
Arduino uno R3 register writing method (1) -- pin level state change
Whistle+switchyomega configure web proxy
Pytorch: tensor operation (I) contiguous
The dolphin scheduler remotely executes shell scripts through the expect command
Imgcat usage experience
.elf .map .list .hex文件
Basic operations of databases and tables ----- modifying data tables
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)