繁体   English   中英

Unity LoadScene太长

[英]Unity LoadScene too long

我正在使用Unity进行游戏,并且正在使用SceneManager.LoadScene从主场景加载到播放场景。 一切都很好,但需要很长时间。 因此,游戏从主场景移动到播放场景,但两个场景之间有一个滑块。

这是我的代码:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Load : MonoBehaviour 
{
    public Slider LoadSlider;
    public Text percentSlider;

    void Start () 
    {
        InvokeRepeating ("AdLoadPercent", 0.01f, 0.4f);
    }

    public void AdLoadPercent()
    {
        LoadSlider.value += Random.Range(0.6f,0.9f);
        percentSlider.text=Mathf.RoundToInt(LoadSlider.value*100).ToString() + " %";

        if (LoadSlider.value >= 1f) 
        {
            SceneManager.LoadScene ("Scena1");
        }
    }
}

当滑块等于1时为什么需要这么长时间?

“长”意味着我必须等待超过15秒。

谢谢和亲切的问候

不使用滑块是否一样?

我没有使用InvokeRepeating ,事实上从来没有听说过,所以有可能这个功能正在发生。

LoadScene()行放在Start()函数中,看看是否有帮助。 如果它立即(或几乎,如<1.25s)切换场景,这是你的重复功能的问题。 对于类似的东西,我建议使用Update()函数或IENumerator

示例#1:

bool loadingStarted = false;
void Start() 
{
    loadingStarted = true;
}

void Update()
{
    if(loadingStarted)
    {
        progressbar.value += Time.deltaTime*0.25f;
        //.. and so on ...
    }
}

示例#2:

void Start()
{
    StartCoroutine(Countdown());
}

IENumerator Countdown()
{
    while(progressBar.value < 1f)
    {
        //Do your incrementation here...
        if(progressBar.value >= 1f) break;
        return yield new WaitForEndOfFrame(); //or WaitForSeconds(0.05);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM