简体   繁体   English

RGB的颜色不正确Unity C#

[英]Colors from RGB are not correct Unity C#

So I have a 2D object that you can change the color of using buttons, and each button will change the sprite of the object. 因此,我有一个2D对象,您可以更改使用按钮的颜色,每个按钮都会更改对象的精灵。 I have a sprite for red, orange, yellow, green, blue, purple. 我有一个红色,橙色,黄色,绿色,蓝色,紫色的精灵。

Now I have a particle system (A child of the object stated before) that I want to change the startColor property of to the same color as whatever the 2D object's sprite is. 现在,我有一个粒子系统(前面提到的对象的子代),我想将startColor属性更改为与2D对象的sprite相同的颜色。 I'm using this code (Attached to the 2D object that you can change the sprite of): 我正在使用此代码(附加到2D对象上,您可以更改其sprite):

        var sprite = gameObject.GetComponent<SpriteRenderer>().sprite;
        var startColor = gameObject.transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().startColor;

        if (sprite == red) startColor = new Color(255, 29, 0);
        else if (sprite == orange) startColor = new Color(254, 32, 0);
        else if (sprite == yellow) startColor = new Color(254, 215, 0);
        else if (sprite == green) startColor = new Color(0, 254, 32);
        else if (sprite == blue) startColor = new Color(0, 37, 254);
        else if (sprite == purple) startColor = new Color(178, 0, 255);

        gameObject.transform.GetChild(0).gameObject.GetComponent<ParticleSystem>().startColor = startColor;

All the new Colors() I'm trying to create from RGB. 我正在尝试从RGB创建的所有新Colors()。 So I took the RGB value of the all color sprites and plugged them in. And when I executed the code, the color of the particles doesn't match the object's sprite color. 因此,我获取了所有彩色子画面的RGB值并将其插入。执行代码时,粒子的颜色与对象的子画面颜色不匹配。

NOTES: I'm running this on android, and the material for the particle system is plain white so it shouldn't tint the color. 注意:我正在android上运行它,并且粒子系统的材质是纯白色,因此它不应该着色。

I put the RGB values from the color sprites in over five times. 我将彩色图片的RGB值放入了五次以上。 But it doesn't match the sprites! 但这与精灵不符! I don't know what's wrong... if you need me to attach the color sprite images, just ask. 我不知道这是怎么回事...如果您需要我附加彩色精灵图像,请询问。

You've misused the Color() constructor . 您滥用了Color()构造函数 Referring to the documentation, take note that parameters for the constructor should be float values in the range of [0,1]. 参考文档,请注意,构造函数的参数应为[0,1]范围内的浮点值。 If you don't want to calculate the appropriate values to supply, just divide them by 255: 如果您不想计算要提供的适当值,只需将它们除以255:

new Color(0, 37/255f, 254/255f);

Alternatively, you may use the Color32() constructor , which takes int values in the range of [0,255]: 另外,您可以使用Color32()构造函数 ,该构造函数的 int值范围为[0,255]:

new Color32(0, 37, 254);

Color and Color32 can be implicitly converted to each other, so don't worry about casting issues. ColorColor32可以相互隐式转换,因此不必担心转换问题。 Hope this helps! 希望这可以帮助! Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

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