Unity 每点击一次按钮生成一个不同的随机数

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40229737/article/details/99572104
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateRandom : MonoBehaviour {


    //List列表存放生成的随机数
    public List<int> Range_NumberList = new List<int>();
    //生成的随机数
    int temp_number;
    //产生数字存放到List里
    public void CreateNum()
    {
        while (Range_NumberList.Count < 10)//由这个循环限制点击次数不能大于10
        {
            temp_number = Random.Range(0, 10);//随机数取值范围
            if (!Range_NumberList.Contains(temp_number))
            {
                Range_NumberList.Add(temp_number);
                Debug.LogError(temp_number);
                return;//找到了满足条件的随机数停止循环函数

            }
            else
            {
                continue;

            }
        }
        if (Range_NumberList.Count >= 10)
        {
            Debug.LogError("超出了点击次数");

        }
    }


 

}

猜你喜欢

转载自blog.csdn.net/qq_40229737/article/details/99572104