当前位置:网站首页>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”程序即可运行。
边栏推荐
- The dolphin scheduler remotely executes shell scripts through the expect command
- .elf .map .list .hex文件
- Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
- Page performance optimization of video scene
- MySQL占用内存过大解决方案
- open-mmlab labelImg mmdetection
- 單片機藍牙無線燒錄
- (四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
- Get the position of the nth occurrence of the string
- Basic operations of databases and tables ----- view data tables
猜你喜欢
CUDA C programming authoritative guide Grossman Chapter 4 global memory
【ESP32学习-1】Arduino ESP32开发环境搭建
JS变量类型以及常用类型转换
Kconfig Kbuild
Types de variables JS et transformations de type communes
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
MySQL占用内存过大解决方案
JS数组常用方法的分类、理解和运用
Amba, ahb, APB, Axi Understanding
(1) Introduction Guide to R language - the first step of data analysis
随机推荐
程序设计大作业:教务管理系统(C语言)
Use of lists
Pat 1097 duplication on a linked list (25 points)
Types de variables JS et transformations de type communes
dosbox第一次使用
Dead loop in FreeRTOS task function
Symbolic representation of functions in deep learning papers
关于Gateway中使用@Controller的问题
Basic operations of databases and tables ----- view data tables
Expected value (EV)
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
ESP学习问题记录
CUDA C programming authoritative guide Grossman Chapter 4 global memory
AMBA、AHB、APB、AXI的理解
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
js题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
Talking about the startup of Oracle Database
Learning notes of JS variable scope and function
Basic operations of databases and tables ----- creating data tables