简体   繁体   English

Unity LoadScene太长

[英]Unity LoadScene too long

I'm making game with Unity and I'm using SceneManager.LoadScene for loading from main scene to play scene. 我正在使用Unity进行游戏,并且正在使用SceneManager.LoadScene从主场景加载到播放场景。 Everything is fine, but it takes too long time. 一切都很好,但需要很长时间。 So, game move from main scene to play scene but there is a slider between two scenes. 因此,游戏从主场景移动到播放场景,但两个场景之间有一个滑块。

This is my code: 这是我的代码:

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");
        }
    }
}

Why it take so long when my slider is equal 1? 当滑块等于1时为什么需要这么长时间?

" long " means that I have to wait more than 15 seconds. “长”意味着我必须等待超过15秒。

Thanks and kind regards 谢谢和亲切的问候

Is it the same without using the slider? 不使用滑块是否一样?

I haven't used InvokeRepeating , never even heard about it in fact, so there's a chance that there's something going on with this function. 我没有使用InvokeRepeating ,事实上从来没有听说过,所以有可能这个功能正在发生。

Place LoadScene() line in the Start() function and see if this helps. LoadScene()行放在Start()函数中,看看是否有帮助。 If it switches scenes instantly (or almost, like in < 1.25s) it's a problem with your repeating function. 如果它立即(或几乎,如<1.25s)切换场景,这是你的重复功能的问题。 For things like that I recommend using an Update() function or an IENumerator 对于类似的东西,我建议使用Update()函数或IENumerator

Example #1: 示例#1:

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

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

Example #2: 示例#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.

相关问题 Unity SyncVar字符串太长? - Unity SyncVar string too long? SceneManager.LoadScene在Unity 4.6中不起作用? - SceneManager.LoadScene does not work in Unity 4.6? Unity中的LoadScene()函数何时更改场景? - When does the LoadScene() function in Unity change the scene? Unity c# SceneManager LoadScene 未从 Task.Delay 运行 - Unity c# SceneManager LoadScene not running from Task.Delay Unity中不对称加密的最大长度和消息太长错误 - Max Length and Message Too Long error for Asymmetric Encryption in Unity 统一之后,在LoadScene之后,是否有一种通用方法可以等待所有monobehaviour#start完成 - In unity, after LoadScene, is there common way to wait all monobehaviour#start to finish LoadScene始终在编辑器中工作,但仅适用于Build:Unity中的第一个播放 - LoadScene always works in Editor, but only on first play-through in Build: Unity Unity 警告 CS0618:“Application.LoadLevel(string)”已过时:“使用 SceneManager.LoadScene” - Unity warning CS0618: 'Application.LoadLevel(string)' is obsolete: 'Use SceneManager.LoadScene' SceneManager.LoadScene(index) 在异步方法中调用时不起作用。 unity3d - SceneManager.LoadScene(index) doesn't work when called in an async method. unity3d 从外部事件调用时,Unity SceneManager.LoadScene() 不起作用 - Unity SceneManager.LoadScene() does not work when being called from external event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM