向给按钮添加一个按下后改变颜色的效果,可以简单地使用以下代码:
public void PressedChangeColor(Button btn){
Text text = btn.GetComponentInChildren<Text>();
text.color = Color.red;
}
今天想让文字的颜色变成一个自定义的颜色(无法通过调用Color.颜色获取)
先从调出想要的颜色,随后获取RGBA值
输入以下代码:
Color pressedColor = new Color(163, 255, 255, 255);
public void PressedChangeColor(Button btn){
Text text = btn.GetComponentInChildren<Text>();
text.color = pressedColor;
}
运行后点击按钮,文字变成了白色,并非预期结果。
经查找,发现原因在于Color的RGBA是0 - 1之间的数,即需要归一化
Unity API - Color
修改代码:
Color pressedColor = new Color(163 / 255, 1, 1, 1);
public void PressedChangeColor(Button btn){
Text text = btn.GetComponentInChildren<Text>();
text.color = pressedColor;
}
运行后点击按钮,文字变成了目标颜色。