当前位置:网站首页>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”程序即可运行。


边栏推荐
- 如何给Arduino项目添加音乐播放功能
- Esp8266 connect onenet (old mqtt mode)
- Cannot change version of project facet Dynamic Web Module to 2.3.
- Common DOS commands
- [899]有序队列
- Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
- PT OSC deadlock analysis
- Cannot change version of project facet Dynamic Web Module to 2.3.
- JS正则表达式基础知识学习
- Servlet
猜你喜欢

Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0

ES6语法总结--下篇(进阶篇 ES6~ES11)

Kaggle competition two Sigma connect: rental listing inquiries

Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer

Conditional probability
![[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree](/img/6e/0802a92511ac50a652afa1678ad28c.jpg)
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree

ES6 grammar summary -- Part I (basic)

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

Whistle+switchyomega configure web proxy

Fashion Gen: the general fashion dataset and challenge paper interpretation & dataset introduction
随机推荐
Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
How to add music playback function to Arduino project
STM32 how to locate the code segment that causes hard fault
Who says that PT online schema change does not lock the table, or deadlock
ES6语法总结--下篇(进阶篇 ES6~ES11)
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Redis 缓存更新策略,缓存穿透、雪崩、击穿问题
Gateway 根据服务名路由失败,报错 Service Unavailable, status=503
Pytorch: tensor operation (I) contiguous
Embedded startup process
Common properties of location
About using @controller in gateway
GCC compilation options
2021.11.10 compilation examination
Time slice polling scheduling of RT thread threads
By v$rman_ backup_ job_ Oracle "bug" caused by details
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
ES6语法总结--上篇(基础篇)
Pat 1097 duplication on a linked list (25 points)