如何随机生成 2 种颜色?

How can I randomly generate 2 colors?

我正在统一制作一个 RISK 游戏。我有 42 个国家的精灵,我必须为每个国家生成随机颜色,例如 2 个玩家 21 绿色和 21 红色

如何随机生成颜色?

{
    this.GetComponent<SpriteRenderer>().color = Color.green;
}
    else
{

    this.GetComponent<SpriteRenderer>().color = Color.red;
}

这可以通过 Unity 的 UnityEngine.Random.Range 功能轻松完成。 生成 01 之间的随机数。使 0 为红色,1 为绿色。

Color getRandomColor()
{
    //0 = red
    //1 = green
    if (UnityEngine.Random.Range(0, 2) == 0)
        return Color.red;
    else
        return Color.green;
}

用法:

this.GetComponent<SpriteRenderer>().color = getRandomColor();

编辑:

I deploy them equally

我猜你的意思是平均生成随机数。 12红12绿同时随机.

使用这个 class:

public class EquallyColorGen
{
    const int totalNumber = 24;

    static bool[] randomNumbers = new bool[totalNumber];
    static Color[] randomColor = new Color[totalNumber];

    public static Color[] generateRandomNumbers()
    {
        const int halfOfTotalNumber = totalNumber / 2;

        int firstRandom = UnityEngine.Random.Range(0, 2);

        bool first12;
        bool last12;

        if (firstRandom == 0)
        {
            first12 = true;
            last12 = false;
        }
        else
        {
            first12 = false;
            last12 = true;
        }

        //Generate Even Random number. The first 12 are the-same. The last 12 are different but the-same
        for (int i = 0; i < randomNumbers.Length; i++)
        {
            randomNumbers[i] = (i < halfOfTotalNumber) ? first12 : last12;
        }

        //Shuff Amount
        const int shuffleAmount = 1;

        for (int j = 0; j < shuffleAmount; j++)
        {
            for (int i = 0; i < halfOfTotalNumber; i++)
            {
                //0 = flip color
                int randColor = UnityEngine.Random.Range(0, 2);
                bool flip = false;

                flip = (randColor == 0) ? true : false;

                //Also flip the 1 to 12
                randomNumbers[i] = (flip == true) ? !randomNumbers[i] : randomNumbers[i];

                //Also flip the other side 12 t0 24
                int lIndex = i + halfOfTotalNumber;
                randomNumbers[lIndex] = (flip == true) ? !randomNumbers[lIndex] : randomNumbers[lIndex];
            }
        }

        //Finally Make color from the list
        for (int i = 0; i < randomNumbers.Length; i++)
        {
            //Red = false
            //Green = true
            randomColor[i] = (randomNumbers[i] == false) ? Color.red : Color.green;
        }

        return randomColor;
    }
}

用法:

Color[] randomPlayerColors = EquallyColorGen.generateRandomNumbers();

24种随机颜色在randomPlayerColors变量中,均等生成。如果您想要 42 种随机颜色,只需将 const int totalNumber = 24; 更改为 const int totalNumber = 42;

好吧,我会采用不同的方法。

一个控制器对象(不是精灵,只是一个不可见的对象,它将包含一些必要的脚本,如在编辑器游戏逻辑界面中)应该有一个脚本,该脚本将获取所有国家游戏对象并在列表中随机洗牌并划分有多少球员的颜色(比如上半场给球员 1,下半场......)

要随机播放列表:

List<CountryScript> countryList = new  List<CountryScript>();
countryList.AddRange(GetComponents<CountryScript>());
List<CountryScript> shuffledCountryList = new  List<CountryScript>();
while(countryList.Count > 0) {
    CountryScript c = countryList[Random.Range(0, countryList.Count)];
    shuffledCountryList.Add(c);
    countryList.Remove(c);
}

最后你有一个空的 countryList 和完整的 shuffledCountryList。您可以在 foreach 循环中进行任何修改,例如:

int playerCount = 2;
int maxNum = shuffledCountryList.Count / playerCount;
//It can be 2 or whatever you'll have it later
for(int i = 0; i < shuffledCountryList.Count; i++) {
    shuffledCountryList[i].SetPlayer(i/maxNum);
}

CountryScript 应该有一个 public void SetPlayer(int Num) {} 方法来为玩家着色由 int Num 值指定的特定颜色。您是将此列表存储在 属性 中的控制器脚本中,以便能够在编辑器中对其进行调整,还是将其硬编码在 CountryScript 中,这是您的选择。我会先走,因为这样你就可以轻松添加更多玩家+编辑而无需编码。或者像程序员写的那样:

(Num == 0) ? Color.red : (Num == 1) Color.green : Color.blue;

希望对您有所帮助。