4

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.

Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156

1 Answers1

7

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());
    }
}
Chen Hao
  • 136
  • 5
  • thank you , but I want change label size , position to top right of screen – Mina Fawzy Aug 27 '15 at 11:24
  • You can use the Rect parameter of GUI.Label() to specify the coordinates. To fit the screen size automatically, you can use the Screen.width variable to make the calculation. – Chen Hao Aug 27 '15 at 14:54
  • 1
    `void OnGUI() { if (loadingStarted) { GUIStyle style = new GUIStyle(); style.normal.textColor = Color.yellow; style.fontSize = 30; style.alignment = TextAnchor.UpperRight; GUI.Label(new Rect(Screen.width - 100, 0, 100, 40), secondsLeft.ToString(), style); } }` – Chen Hao Aug 27 '15 at 15:01