我正在使用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秒。
谢谢和亲切的问候
答案 0 :(得分:0)
不使用滑块是否相同?
我还没有使用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);
}
}