【问题标题】:Unity LoadScene too longUnity LoadScene 太长
【发布时间】:2016-03-12 21:30:02
【问题描述】:

我正在使用 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 时需要这么长时间?

“long”表示要等15秒以上。

感谢和亲切的问候

【问题讨论】:

  • 此外,如果值 >=1,您可能希望停止递增,因为会一遍又一遍地调用 LoadScene()

标签: c# unity3d


【解决方案1】:

不使用滑块也一样吗?

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

LoadScene() 行放在Start() 函数中,看看是否有帮助。如果它立即切换场景(或几乎,如 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);
    }
}

【讨论】:

  • 您从未听说过InvokeRepeating?太棒了。它可能是 Unity 中最常用的功能。了解协程很棒,但从未遇到过InvokeInvokeRepeating
  • 嘿,当我将 SceneManager.LoadScene ("Scena1") 启动功能时,我遇到了同样的问题。我只有一个带有那个滑块的场景,所以主菜单->滑块->播放场景
  • 尝试在主场景中添加滑块。创建一个带有填充整个画布的黑色 UI 图像的预制件,以及角落中的滑块或其他东西。您可以在按下按钮后实例化预制件,并将其父级设置为:loading.transform.SetParent(GameObject.Find("Canvas"), false)。如果从主菜单到滑块的转换可以立即工作,那应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 2021-12-04
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-11
  • 1970-01-01
相关资源
最近更新 更多