如何在Unity3D中使用C#延迟?

Fac*_*ann 0 c# unity-game-engine

我刚刚开始在团结中学习c#.我按照教程,但我想添加一些东西.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText ();
        winText.text = "";
    }

    void FixedUpdate ()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

        rb.AddForce (movement * speed);
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            SetCountText ();
        }
    }

    void SetCountText ()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 1)
        {
            winText.text = "You Win!";
            Application.LoadLevel(1);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我想在winText.text ="你赢了!"之间做一个延迟.和Application.LoadLevel(1); 所以你实际上可以阅读文本.我希望有人可以帮助我!

Jer*_*ski 5

使用Coroutine(我看到这是Unity3D代码):

void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.CompareTag ( "Pick Up"))
        {
            other.gameObject.SetActive (false);
            count = count + 1;
            StartCoroutine(SetCountText ());
        }
    }

    IEnumerator SetCountText ()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 1)
        {
            winText.text = "You Win!";
            yield return new WaitForSeconds(1f);
            Application.LoadLevel(1);
        }
    }
Run Code Online (Sandbox Code Playgroud)