在Unity中加载酒吧场景

时间:2018-10-20 21:52:11

标签: c# unity3d asynchronous loading progress

我的Unity项目中有2个场景。第一个是加载场景,由一个简单的文本和一个滑动条组成。第二个场景是“地形”(3个大小为1x1x1的立方体)的随机生成器。

加载第二个场景所需的时间约为15秒,但是当我使用AsyncOperation中的方法progress时,我只会得到0或0.9的值。

这是我的加载栏脚本:

IEnumerator LoadingScreen(int level)
{
    button.gameObject.SetActive(false);
    loadingScreenObj.SetActive(true);

    AsyncOperation async = SceneManager.LoadSceneAsync(level);
    async.allowSceneActivation = false;
    Debug.Log(async.progress);

    while(!async.isDone){
        slider.value = async.progress;
        Debug.Log(slider.value);
        if (async.progress >= 0.9f){
            slider.value = 1f;
            async.allowSceneActivation = true;
        }
        yield return null;
    }
}

地形生成器的脚本是:

public class SpawnInitialFloor : MonoBehaviour {

public GameObject [] prefabs;
private GameObject floor;
List<Vector2> list = new List<Vector2>();
private float y = 0;
private float yWater = -0.1f;
public int maxIterations = 50;
private int max = 1;
private int min = -1;

// Use this for initialization
void Start (){
    setSpawn(); 
}

private void setSpawn()
{
    int r;
    for (int i = 0; i < maxIterations; ++i){
        for (int xIt = min; xIt <= max; ++xIt){
            for (int zIt = min; zIt <= max; ++zIt){
                if (!list.Contains(new Vector2(xIt, zIt))){
                    r = Random.Range(0, 3);
                    if (r == 1)
                        floor = Instantiate(prefabs[r], new Vector3(xIt, yWater, zIt), Quaternion.identity) as GameObject;
                    else
                        floor = Instantiate(prefabs[r], new Vector3(xIt, y, zIt), Quaternion.identity) as GameObject;

                    list.Add(new Vector2(xIt, zIt));
                }
            }
        }
        max += 1;
        min -= 1;
    }
}
}

在其他场景加载大约15秒钟的过程中,加载栏始终像这样:Loading Bar

在第二个场景的完整加载之前和之后: Before-After

我想知道是否需要更改SpawnInitialFloor脚本中的某些内容或使用其他内容代替AsyncOperation的进度方法。

0 个答案:

没有答案