RGB颜色不正确Unity C# [英] Colors from RGB are not correct Unity C#

查看:587
本文介绍了RGB颜色不正确Unity C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个2D对象,你可以改变使用按钮的颜色,每个按钮将改变对象的精灵。我有一个红色,橙色,黄色,绿色,蓝色,紫色的精灵。



现在我有一个粒子系统将startColor属性更改为与2D对象的sprite是相同的颜色。我使用这个代码(附加到2D对象,你可以改变精灵):

  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;

所有新的Colors()我想从RGB创建。所以我把所有颜色的精灵的RGB值和插入他们。当我执行代码,粒子的颜色不匹配对象的sprite颜色。



我把RGB的颜色设置为值从颜色sprites五倍以上。但它不匹配精灵!

解决方案

你不知道是什么问题?如果你需要我附加颜色精灵图片, 误用 Color()构造函数。参考文档,注意构造函数的参数应该是[0,1]范围内的浮点值。如果你不想计算合适的值,只需将它们除以255:

  / 255f,254 / 255f); 

或者,您可以使用 Color32()构造函数,其取值范围为[0,255] p>

  new Color32(0,37,254); 

Color Color32 可以隐式转换为彼此,因此不必担心投射问题。希望这可以帮助!如有任何问题,请与我们联络。


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. 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. I'm using this code (Attached to the 2D object that you can change the sprite of):

        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. 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.

NOTES: I'm running this on android, and the material for the particle system is plain white so it shouldn't tint the color.

I put the RGB values from the color sprites in over five times. 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. Referring to the documentation, take note that parameters for the constructor should be float values in the range of [0,1]. If you don't want to calculate the appropriate values to supply, just divide them by 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]:

new Color32(0, 37, 254);

Color and Color32 can be implicitly converted to each other, so don't worry about casting issues. Hope this helps! Let me know if you have any questions.

这篇关于RGB颜色不正确Unity C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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