Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity3D:生成随机颜色组合-颜色开关轮_C#_Unity3d - Fatal编程技术网

C# Unity3D:生成随机颜色组合-颜色开关轮

C# Unity3D:生成随机颜色组合-颜色开关轮,c#,unity3d,C#,Unity3d,我正在创建一个类似于颜色切换游戏的游戏,我创建了这个脚本来生成8种颜色的随机颜色组合。我把轮子劈成了两半。当我按下按钮时,我希望每个零件都有不同的颜色,然而,经过多次尝试,我仍然得到两个或更多零件具有相同颜色的问题。这能做到吗 要演示的gif: (在本例中,我单击按钮5次,在两种情况下,相同的颜色重复两次,即紫色和粉色。) 我的代码: public SpriteRenderer[] colorWheels; public Color[] colorcombination; public

我正在创建一个类似于颜色切换游戏的游戏,我创建了这个脚本来生成8种颜色的随机颜色组合。我把轮子劈成了两半。当我按下按钮时,我希望每个零件都有不同的颜色,然而,经过多次尝试,我仍然得到两个或更多零件具有相同颜色的问题。这能做到吗

要演示的gif:

(在本例中,我单击按钮5次,在两种情况下,相同的颜色重复两次,即紫色和粉色。)

我的代码:

 public SpriteRenderer[] colorWheels;
 public Color[] colorcombination;

public int uniqueRandomInt(int min, int max)
{
    int result = Random.Range(min, max);

    if(result == lastRandomNumber) {

        return uniqueRandomInt(min, max);

    }
    
    lastRandomNumber = result;
    return result;
}

public  void switchColour()
{
    for(int i = 0; i < colorWheels.Length; i ++)
    {
      colorWheels[i].material.color = RANDcolorcombination[uniqueRandomInt(0,8)];
    }
}
publicsspriteender[]色轮;
公共色彩[]色彩组合;
公共整数uniqueRandomInt(整数最小值,整数最大值)
{
int结果=随机范围(最小值、最大值);
如果(结果==lastRandomNumber){
返回uniqueRandomInt(最小值、最大值);
}
lastRandomNumber=结果;
返回结果;
}
公共空间颜色()
{
对于(int i=0;i

谢谢大家!

听起来你真正想要的是随机选择每个按钮的颜色,然后从中选择4种颜色:

private System.Random random = new System.Random();

public  void switchColour()
{
    // Is shuffled but it is sure that it still contains the same elements
    var randomizedColors = colorcombination.OrderBy(c => random.Next()).ToArray();

    // You can now simply select the first 4 values from the shuffled array 
    for(int i = 0; i < colorWheels.Length; i ++)
    {
      colorWheels[i].material.color = randomizedColors[i];
    }
}
private System.Random Random=new System.Random();
公共空间颜色()
{
//已洗牌,但它肯定仍包含相同的元素
var randomizedColors=colorcombination.OrderBy(c=>random.Next()).ToArray();
//现在,您只需从无序数组中选择前4个值
对于(int i=0;i

这可确保每次单击时所选的颜色都是唯一的。这是不太可能的,但如果点击次数足够多,仍然会发生,当然,一套完整的4种颜色会出现两次;)

你想从8种颜色中选择4种(不同的)颜色吗?是的,这就是我想要的结果。要么删除已选择的颜色,要么拒绝已选择的颜色,如果已在已选择的列表中,为什么我要删除这些颜色?我想在点击每个按钮时使用不同的颜色组合,但我不想去掉任何颜色。因此,第一次点击按钮可以是蓝色、紫色、黄色和红色,然后下一次点击按钮可以是红色、粉色、绿色和紫色(这仍然很好),但就像第三次点击按钮一样(例如),我不想要红色、红色,黄色,蓝色。(红色重复)谢谢,这很有帮助!