使用StartCoroutine或Application.LoadLevel更改场景 [英] Changing scenes with StartCoroutine or Application.LoadLevel

查看:159
本文介绍了使用StartCoroutine或Application.LoadLevel更改场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Unity项目中更改场景时,例如,通过按一个按钮将菜单场景更改为主场景,以下其中一种方式更为明智:

When changing scenes in a Unity project, say, from a Menu Scene to a Main Scene by pressing a button, which one of the following would be a more sensible way:

Application.LoadLevel( ("mainScene") );

StartCoroutine( LoadLevel("mainScene") );

按钮后面的代码是以上两种方法之一,以及要实现的大量其他方法,所以这就是为什么我想知道使用哪种方法来更改场景...

The code behind the button is either of the above, as well as a huge number of other methods to be implemented, so that is why I am wondering which one to use for changing scenes...

推荐答案

使用StartCoroutine更好,因为如果您在单击按钮并尝试立即更改场景时播放声音,则声音会被剪切,具体取决于有关如何配置GameObject和控制声音的脚本的信息.

Using StartCoroutine is better imo because if you play a sound when you click a button and try to change the scene right away, the sound will be cut depending on how you configured the GameObjects and scripts that control sound.

这是我使用的脚本.请注意,我会花0.2秒使声音结束.

This is the script I use. Note that I give 0.2 seconds for the sound to finish.

using UnityEngine;
using System.Collections;

    public class SceneLoader : MonoBehaviour
    {
        private AudioController audioController;

        void Start ()
        {
            audioController = GameObject.FindWithTag ("AudioController").GetComponent<AudioController> ();
        }

        public void LoadLevel (string sceneName)
        {
            audioController.PlayMenuClick ();
            StartCoroutine (LoadSceneAsync (sceneName));
        }

        private IEnumerator LoadSceneAsync (string levelName)
        { 
            yield return new WaitForSeconds (0.2f);
            Application.LoadLevel (levelName);
        }
    }

这篇关于使用StartCoroutine或Application.LoadLevel更改场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆