利用Text和InputField以及协程编写倒计时功能,

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

public class CORtIMER : MonoBehaviour {
InputField inputF;
Text text;
float time;
// Use this for initialization
void Start () {
//获取InputField组件
inputF = transform.Find(“InputField”).GetComponent();
//获取Text组件
text = transform.Find(“Text”).GetComponent();
inputF.onEndEdit.AddListener(EndEdit);//添加调用事件

}
// Update is called once per frame
void Update () {
}
void EndEdit(string text)
{
    float tmpTime = 0;

    if (float.TryParse(text,out tmpTime))//将text转化为flote类型并赋值给tmpTime
    {
        if (time <= 0)
        {
            time = tmpTime;
            StartCoroutine(TimerCor());
        }
        else
        {
            time = tmpTime;
        }
    }

}
IEnumerator TimerCor()
{
    for (;  ;)
    {

        if (time<=0)
        {

            text.text = "0";
            break;
        }
        text.text = time.ToString();

        time -= Time.deltaTime;
        yield return null;
    }

}

}

你可能感兴趣的:(Text,InputField,协程,text,InputField,携程)