Unity LoadScene too long

I'm making game with Unity and I'm using SceneManager.LoadScene for loading from main scene to play scene. 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?

" long " means that I have to wait more than 15 seconds.

Thanks and kind regards

Answers


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.

Place LoadScene() line in the Start() function and see if this helps. If it switches scenes instantly (or almost, like in < 1.25s) it's a problem with your repeating function. For things like that I recommend using an Update() function or an IENumerator

Example #1:

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

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

Example #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);
    }
}

Need Your Help

Append to a serialization object

r

Let's say that you have a data object saved to rds. Is there a way to append (like c(), or rbind, cbind) to the saved object without explicitly loading the original? Given the assumption that the o...

UITapGestureRecognizer on UIImageView within UITablevlewCell not getting called

ios uitableview gesture tap

I currently have a custom UITableViewCell which contains a UIImageView and trying to add a UITapGestureRecognizer on the UIImageView with no luck. here is snippet of the code.