Unity - change scene after specific time

折月煮酒 提交于 2019-12-02 00:01:33

问题


I am developing game for oculus Gear VR (to put in your consideration memory management ) and I need to load another screen after specific time in seconds

void Start () {

    StartCoroutine (loadSceneAfterDelay(30));

    }

    IEnumerator loadSceneAfterDelay(float waitbySecs){

        yield return new WaitForSeconds(waitbySecs);
        Application.LoadLevel (2);
    } 

it works just fine ,

my questions :

1- What are the best practices to achieve this?

2- How to display timer for player showing how many seconds left to finish level.


回答1:


Yes, it is the correct way. Here's the sample code to display a countdown message:

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour
{
    bool loadingStarted = false;
    float secondsLeft = 0;

    void Start()
    {
        StartCoroutine(DelayLoadLevel(10));
    }

    IEnumerator DelayLoadLevel(float seconds)
    {
        secondsLeft = seconds;
        loadingStarted = true;
        do
        {
            yield return new WaitForSeconds(1);
        } while (--secondsLeft > 0);

        Application.LoadLevel("Level2");
    }

    void OnGUI()
    {
        if (loadingStarted)
            GUI.Label(new Rect(0, 0, 100, 20), secondsLeft.ToString());
    }
}


来源:https://stackoverflow.com/questions/32243811/unity-change-scene-after-specific-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!