[英]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.