C# 如何随机生成2种颜色?

C# 如何随机生成2种颜色?,c#,unity3d,random,colors,sprite,C#,Unity3d,Random,Colors,Sprite,我在团结中做一个冒险游戏。我有42个国家的精灵,我必须为每个国家生成随机颜色,例如为2个玩家生成21个绿色和21个红色 如何随机生成颜色 { this.GetComponent<SpriteRenderer>().color = Color.green; } else { this.GetComponent<SpriteRenderer>().color = Color.red; } { this.GetComponent().color=co

我在团结中做一个冒险游戏。我有42个国家的精灵,我必须为每个国家生成随机颜色,例如为2个玩家生成21个绿色和21个红色

如何随机生成颜色

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

    this.GetComponent<SpriteRenderer>().color = Color.red;
}
{
this.GetComponent().color=color.green;
}
其他的
{
this.GetComponent().color=color.red;
}

这可以通过Unity的功能轻松完成。。 生成0和1之间的随机数。将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();

randomPlayerColors
变量中有24种随机颜色,它们是均匀生成的。如果您想要42种随机颜色,只需更改
const int totalNumber=24到<代码>常量整数总数=42

我会采取不同的方法

控制器对象(不是精灵,只是一个不可见的对象,可以像编辑器游戏逻辑界面那样保存一些必要的脚本)应该有一个脚本,该脚本可以获取所有国家/地区的游戏对象,并在列表中随机洗牌,并在有多少玩家的颜色之间进行划分(如前半部分为玩家1,后半部分为玩家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);
}

希望这对您有所帮助。

您也可以使用
Random.value
Color c=Random.value<0.5f?Color.red:Color.blue
@Iggy是的,你可以。我本来可以的,但是通过阅读这个问题,OP对C#来说似乎是新的,我认为
if
语句将使我更容易理解我所做的事情,而不是三元运算符。谢谢,是的,我对unity C#来说是新的,它帮助很大,但有一个问题是,绿色国家和红色国家的数量并不相等,有时绿色国家比红色国家多,有时红色国家比绿色国家多,我如何才能平等地部署它们?他们有“国家”的标签,我怎样才能将他们添加到一个数组中,这样我就可以比较他们了@Programmer42国家精灵,我把它们标记为国家,,,但主要问题是我不能平均分配颜色,21,21每个@ProgrammerCheck我编辑的答案。这很难做到,但答案是存在的。是的,这看起来很好,在一段时间后,我不得不增加更多的球员,但我很少有像这样的错误。我是unity c#的新手。。“错误CS0246找不到类型或命名空间名称‘列表’(是否缺少using指令或程序集引用?)”错误CS0246找不到类型或命名空间名称‘CountryScript’(是否缺少using指令或程序集引用?)”,我在我的问题中发布了一个截图@zORg AlexWell yes。列表位于System.Collections.Generic中。您需要添加一个using System.Collections.Generic;以及大约第二个。您需要创建一个具有该名称的新脚本并附加到您的国家/地区对象。正如我在回答中所说的。我不打算编写所有内容。我希望您尝试这样做做吧。不过我可以帮你。你需要在unity教程中学习脚本。它们会解释很多好的东西。比如如何组织脚本,如何做简单的事情。我更喜欢在游戏中几乎所有游戏对象类型都有一个脚本。通过GetComponent()可以帮助在代码中区分它们+在任何时候都可以很容易地向其中任何一个添加一些功能。+使用MS Visual Studio。Ctrl+'.'将帮助您解决问题。使用Ctrl+'.'添加所需的用法甚至生成新类都很容易。Unity脚本将更复杂,因为它们需要设置为public,派生自MonoBehavior等。我不确定如何使用它设置MonoBehavior template.Hmm…这是一个好问题。只剩下一个错误,错误CS1061“Spriterender”不包含“SetPlayer”的定义,并且找不到接受“Spriterender”类型的第一个参数的扩展方法“SetPlayer”(是否缺少using指令或程序集引用?
Color[] randomPlayerColors = EquallyColorGen.generateRandomNumbers();
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);
}
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);
}
(Num == 0) ? Color.red : (Num == 1) Color.green : Color.blue;