当前位置:网站首页>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”程序即可运行。
边栏推荐
- Dead loop in FreeRTOS task function
- Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
- 程序员老鸟都会搞错的问题 C语言基础 指针和数组
- Common properties of location
- 2022.2.12 resumption
- Pytorch: tensor operation (I) contiguous
- Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
- Basic operations of databases and tables ----- classification of data
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
- Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
猜你喜欢
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
Amba, ahb, APB, Axi Understanding
编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
js 变量作用域和函数的学习笔记
NRF24L01故障排查
NRF24L01 troubleshooting
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
随机推荐
Esp8266 connects to bafayun (TCP maker cloud) through Arduino IED
Single chip Bluetooth wireless burning
单片机蓝牙无线烧录
Flink late data processing (3)
Classification, understanding and application of common methods of JS array
Rough analysis of map file
【ESP32学习-1】Arduino ESP32开发环境搭建
Arm pc=pc+8 is the most understandable explanation
1081 rational sum (20 points) points add up to total points
Arduino gets the length of the array
JS variable types and common type conversions
MySQL takes up too much memory solution
CUDA C programming authoritative guide Grossman Chapter 4 global memory
JS正则表达式基础知识学习
Detailed explanation of truncate usage
PT OSC deadlock analysis
基于Redis的分布式锁 以及 超详细的改进思路
ESP8266连接onenet(旧版MQTT方式)
單片機藍牙無線燒錄
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports