using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class AirChange : MonoBehaviour { // Update is called once per frame void OnEnable() { StartCoroutine(ChangeAir()); } private IEnumerator ChangeAir() { while (true) // 无限循环 { string text = gameObject.GetComponent().text; float air = float.Parse(text); if (Random.Range(0, 10) < 2&&air>15)air=air-0.1f; if (Random.Range(0, 10) >7&&air<35)air=air+0.1f; gameObject.GetComponent().text = air.ToString(); int time = Random.Range(2, 5); yield return new WaitForSeconds(time); // 等待两秒 } } }