制作一个数字数组5, 6, 7, 8, 9, 10, 20, 21, 22, 23, 24, 25
然后从数组中随机选择一个数字。
void Awake()
{
int[] array = {5, 6, 7, 8, 9, 10, 20, 21, 22, 23, 24};
int randNum = Random.Range(0, array.Length);
}
编辑:我看到您将数字作为一组,但如果您只需要一组 x 和一组 y。然后
Vector3 position = new Vector3(Random.Range(5,11), Random.Range(10, 26), z);
注意最后一个数字是 n+1,因为 Random.Range 是独占的。
EDIT2:使用我的课程,它将允许您通过 3 个范围并随机返回一个数字。
using UnityEngine;
using System.Collections;
public class RandomSetGenerator
{
public static int generateFromRange(int range1Start, int range1Stop, int range2Start, int range2Stop, int range3Start, int range3Stop)
{
List<Integer> randomList = new List<Integer>();
randomList = addToRandomList(randomList, range1Start, range1Stop);
randomList = addToRandomList(randomList, range2Start, range2Stop);
randomList = addToRandomList(randomList, range3Start, range3Stop);
return randomList[Random.Range(0, randomList.Count)];
}
private static List<Integer> addToRandomList(List<Integer> randomList, int startRange, int endRange)
{
for(int i = startRange; i <= endRange; i++)
randomList.Add(i);
return randomList;
}
}