如何获得不重复的随机颜色? [英] How to get random colors without repeating?

查看:110
本文介绍了如何获得不重复的随机颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个,gate"获得一个随机的颜色.所以我有9个门以及9种材料和9种颜色.现在,我希望每个gate都获得一种随机的颜色,但是颜色不能重复.例如不能是2种红色或3种蓝色,每种一种.感谢您的帮助 enter image description here

I want to get a random color for each ,,gate".So i have 9 gates and 9 materials and 9 colors.Now i want that every gate to get a random color but the colors must not repeat.For example there can't be 2 red colors or 3 blue colors,one for each.Thanky for helping enter image description here

这是我尝试过的代码.我有2次尝试.旧版本在注目中

Here is the code that i tryed.I have 2 trys.The old version is in the comentary

public Material[] material;
public Color[] colors;


public void ColorChange()
{
    colors[0] = Color.blue;
    colors[1] = Color.black;
    colors[2] = Color.red;
    colors[3] = Color.green;
    colors[4] = Color.yellow;
    colors[5] = Color.white;
    colors[6] = Color.magenta;
    colors[7] = Color.cyan;
    colors[8] = Color.grey;

    int randomcolor = Random.Range(0, colors.Length);
    int randommaterial = Random.Range(0, material.Length);

    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];
    material[randommaterial].color = colors[randomcolor];

    /*int color1 = Random.Range(0, colors.Length);
    int color2 = Random.Range(0, colors.Length);
    int color3 = Random.Range(0, colors.Length);
    int color4 = Random.Range(0, colors.Length);
    int color5 = Random.Range(0, colors.Length);
    int color6 = Random.Range(0, colors.Length);
    int color7 = Random.Range(0, colors.Length);
    int color8 = Random.Range(0, colors.Length);
    int color9 = Random.Range(0, colors.Length);

    material[0].color = colors[color1];
    material[1].color = colors[color2];
    material[2].color = colors[color3];
    material[3].color = colors[color4];
    material[4].color = colors[color5];
    material[5].color = colors[color6];
    material[6].color = colors[color7];
    material[7].color = colors[color8];
    material[8].color = colors[color9];
    */

推荐答案

通常当然会假定material.Length <= colors.Length-否则将很难实现目标;)

In general assuming of course that material.Length <= colors.Length - otherwise it would be hard to fulfill the goal ;)

您宁愿要做的是随机播放您的列表,并使用

What you rather want to do is shuffle your list and assign them in "shuffled order" using Linq OrderBy

足以随机化两个数组之一

It is enough to randomize one of the two arrays

using System.Linq;
using Random = System.Random;
...

public Material[] material;
public Color[] colors;


public void ColorChange()
{
    if(material.Length > colors.Length)
    {
        Debug.LogError("Not enough colors to have a unique color for each material!", this);
        return;
    }

    colors = new [] {
        Color.blue, 
        Color.black, 
        Color.red, 
        Color.green, 
        Color.yellow, 
        Color.white, 
        Color.magenta, 
        Color.cyan, 
        Color.grey };

    var rnd = new Random();
    var randomColors = colors.OrderBy(x => rnd.Next()).ToArray();  

    for(var i = 0; i < material.Length; i++)
    {
        material[i].color = randomColors[i];
    }
}

这篇关于如何获得不重复的随机颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆