31 lines
852 B
C#
31 lines
852 B
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using Random = UnityEngine.Random;
|
|
|
|
|
|
public class RandomNumberInt : MonoBehaviour
|
|
{
|
|
// Start is called before the first frame update
|
|
void OnEnable()
|
|
{
|
|
StartCoroutine(random());
|
|
}
|
|
|
|
private IEnumerator random()
|
|
{
|
|
while (true) // 无限循环
|
|
{
|
|
string fontText = gameObject.GetComponent<TextMeshProUGUI>().text;
|
|
int number = int.Parse(fontText);
|
|
if (Random.Range(0, 10) < 2&&number>0)number--;
|
|
if (Random.Range(0, 10) >7&&number<1000)number++;
|
|
gameObject.GetComponent<TextMeshProUGUI>().text = number.ToString();
|
|
int time = Random.Range(2, 5);
|
|
yield return new WaitForSeconds(time); // 等待两秒
|
|
}
|
|
}
|
|
}
|