如何在Unity3D中使用C#进行延迟? [英] How to make a delay with C# in Unity3D?

查看:1177
本文介绍了如何在Unity3D中使用C#进行延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始统一学习c#.我遵循了教程,但我想添加一些内容.

I just started to learn c# in unity. I followed a tutorial, but i wanna add some things.

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);
        }
    }

}

我想在winText.text ="You Win!"之间延迟一下; 和Application.LoadLevel(1);因此,您实际上可以阅读文本.我希望有人能帮助我!

I wanna make a delay between winText.text = "You Win!"; and Application.LoadLevel(1); So you can actually read the text. I hope somebody can help me out!

推荐答案

使用协程(如我所见,这是Unity3D代码):

Use Coroutine (as I see this is Unity3D code):

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);
        }
    }

这篇关于如何在Unity3D中使用C#进行延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆