简体   繁体   English

Unity:我无法更改图像的不透明度或在 RGB 中更改其颜色

[英]Unity : I can't change the opacity of an image or change its color in RGB

In my project, I want to change the color of a square at runtime which is simply a game object with an Image component.在我的项目中,我想在运行时更改正方形的颜色,这只是一个带有Image组件的游戏 object。 In my script I proceed like this:在我的脚本中,我是这样进行的:

private Image imageRenderer;


imageRenderer = ImageCompleteSquat.GetComponent<Image>();
imageRenderer.color = Color.red;

In this case, it works and the image turns red as expected.在这种情况下,它可以正常工作,并且图像按预期变为红色。 But as soon as I change this line by putting my own RGB color like this:但是一旦我通过像这样放置自己的 RGB 颜色来更改此行:

imageRenderer.color = new Color(227, 66, 52);

The image is not displayed anymore: it disappears.图像不再显示:它消失了。 Does anyone know how to change the color of an Image component?有谁知道如何更改Image组件的颜色?

you might want to set the alpha value first你可能想先设置 alpha 值

imageRenderer = ImageCompleteSquat.GetComponent<Image>();
var tempColor = Color.red;
tempColor.a = 1f; // change this value (floating point between 0 and 1)
imageRenderer.color = tempColor;

From the docs of Color , each color component is a floating point value with a range from 0 to 1 .Color的文档中,每个颜色分量都是一个浮点值,范围从01 Hence just divide the arguments passed into Color() by 255因此只需将传递给Color()的 arguments 除以 255

imageRenderer.color = new Color(227/255f, 66/255f, 52/255f);

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

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