赞
踩
Unity的场景跳转需要用到其自带的场景管理类SceneManage。
想要实现场景跳转首先需要在File-->Build Settings里将你需要用到的场景添加进去。
打开Build Settings
1.当前已添加的场景与其序号,左边是场景名,右边是序号。打勾即为会使用该场景。
2.将当前正在打开的场景(Scene)加入到Build Setting中。
首先要确保你需要跳转的场景已经加入到了Build Setting中,然后便可创建C#场景跳转代码如下:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class Leveltwo : MonoBehaviour
- {
- public void SceneChange()
- {
- SceneManager.LoadScene(你要转跳的Scene的序号);
- }
- }
这里SceneManager.LoadScene()方法中所填入的数字便是你想跳转场景的序号。
这种写法的好处是直接指定序号便可进行场景跳转,简单粗暴,此外还有另一种写法:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class StartMenu : MonoBehaviour
- {
- // Start is called before the first frame update
- public void StartGame()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
- }
-
- }
这种写法是基于当前打开的场景序号进行四则运算后跳转至运算结果序号对应的场景中。若当前打开的场景序号为0,这里buildIndex+1便从序号0的场景+1后跳转到序号1的对应场景。
以上便是两种进行场景跳转的脚本写法。
最后将你写好的脚本添加至对应的触发物体上便可,我自己此处的两个脚本都是添加到了Button物体上,实现点击触发。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。