简体   繁体   English

数组随机颜色不生成

[英]Array random color not generating

I'm facing a problem where when I declare array of random colors. 我在声明随机颜色数组时遇到一个问题。 It shows random colors in a particle system on game start, but every time the game starts it shows white. 它在游戏开始时在粒子系统中显示随机颜色,但是每次游戏开始时它都显示白色。 I don't know why it happens, I didn't set white in my array. 我不知道为什么会这样,我没有在数组中设置白色。

public class A : MonoBehaviour 
{       
   Color[] colors = {
   new Color (170, 7, 107),
   new Color (58, 96, 115),
   new Color(81, 99, 149),
   new Color(71, 118, 231)
};

void start()
{
   GetComponent<ParticleSystem>().startColor =  colors[Random.Range(0, colors.Length)];
}

In Unity, a color's ARGB components range between 0.0 to 1.0. 在Unity中,颜色的ARGB分量在0.0到1.0之间。 So anything >1 will be considered 1 and so all the colors are white naturally. 因此,任何大于1的东西都将被视为1,因此所有颜色自然都是白色。

To convert the colors, divide each component by 255. You can either do this yourself or leave it to the code itself. 要转换颜色,请将每个分量除以255。您可以自己执行此操作,也可以将其留给代码本身。 Also, don't forget to cast as float. 另外,不要忘记将其转换为float。 Credit to @Masih Akbari for reminding me about this. 感谢@Masih Akbari提醒我这一点。

So, it should be : 因此,应为:

Color[] colors = {
    new Color (170f/255, 7f/255, 107f/255),
    new Color (58f/255, 96f/255, 115f/255),
    new Color(81f/255, 99f/255, 149f/255),
    new Color(71f/255, 118f/255, 231f/255)
}

The reason for this is that colours are normalised in Unity. 原因是颜色在Unity中已标准化。 You have to divide each float you've set by 255 to get the actual value, eg 您必须将设置的每个浮点数除以255,以获得实际值,例如

Color[] colors = {

    new Color (170/255, 7/255, 107/255),
    new Color (58/255, 96/255, 115/255),
    new Color(81/255, 99/255, 149/255),
    new Color(71/255, 118/255, 231/255)
};

Your Color values must be between 0 to 1. everything after 1 is considered white. 您的颜色值必须在0到1之间。1以后的所有内容都被视为白色。

Don't forget to cast your number as a float 别忘了将您的数字转换为浮点数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM