当前位置:网站首页>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”程序即可运行。
边栏推荐
- Servlet
- AMBA、AHB、APB、AXI的理解
- Knowledge summary of request
- 【ESP32学习-2】esp32地址映射
- ORA-02030: can only select from fixed tables/views
- Understanding of AMBA, AHB, APB and Axi
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
- Page performance optimization of video scene
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- Conditional probability
猜你喜欢
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
Latex learning
VSCode基础配置
Postman 中级使用教程【环境变量、测试脚本、断言、接口文档等】
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
CUDA C programming authoritative guide Grossman Chapter 4 global memory
Understanding of AMBA, AHB, APB and Axi
Arduino uno R3 register writing method (1) -- pin level state change
随机推荐
ESP8266连接onenet(旧版MQTT方式)
A possible cause and solution of "stuck" main thread of RT thread
MySQL takes up too much memory solution
Servlet
Arduino uno R3 register writing method (1) -- pin level state change
Navigator object (determine browser type)
Learning notes of JS variable scope and function
Esp8266 connect onenet (old mqtt mode)
[esp32 learning-1] construction of Arduino esp32 development environment
[leetcode622]设计循环队列
Imgcat usage experience
Pytoch temperature prediction
[offer78]合并多个有序链表
dosbox第一次使用
Important methods of array and string
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Mysqldump error1066 error solution
Basic operations of databases and tables ----- creating data tables
Arduino gets the length of the array
Kconfig Kbuild