如何在运行时设置 RGBA 值时更改主相机的背景颜色?
How to change background colour of Main Camera in runtime setting RGBA values?
这是我访问和更改主摄像头背景颜色的代码
Camera.main.GetComponent<Camera>().backgroundColor = new Color(228f,234f,241f,0f);
此代码使背景变白,并在检查器中将值设置得非常高。我做错了什么?
颜色结构将浮点数作为参数,因此您需要将颜色转换为 0.0 - 1.0。
例如
Camera.main.backgroundColor = new Color(0f, 1f, 0f);
颜色值被归一化,即它们的范围从 0 到 1 (see the documentation here)。因此,将那里的每个值除以 255 以获得实际值。
编辑:按照@FarhanAnam 的建议,您可以按照以下方式进行操作。
Camera.main.GetComponent<Camera>().backgroundColor = new Color(228f / 255f, 234f / 255f, 241f / 255f, 0f);
无需划分 - 只需使用 Color32
中内置的单位:
Camera.main.GetComponent<Camera>().backgroundColor = new Color32(228,234,241,0);
请注意,Color32
构造函数需要 byte
类型的操作数而不是浮点数。
这是我访问和更改主摄像头背景颜色的代码
Camera.main.GetComponent<Camera>().backgroundColor = new Color(228f,234f,241f,0f);
此代码使背景变白,并在检查器中将值设置得非常高。我做错了什么?
颜色结构将浮点数作为参数,因此您需要将颜色转换为 0.0 - 1.0。
例如
Camera.main.backgroundColor = new Color(0f, 1f, 0f);
颜色值被归一化,即它们的范围从 0 到 1 (see the documentation here)。因此,将那里的每个值除以 255 以获得实际值。
编辑:按照@FarhanAnam 的建议,您可以按照以下方式进行操作。
Camera.main.GetComponent<Camera>().backgroundColor = new Color(228f / 255f, 234f / 255f, 241f / 255f, 0f);
无需划分 - 只需使用 Color32
中内置的单位:
Camera.main.GetComponent<Camera>().backgroundColor = new Color32(228,234,241,0);
请注意,Color32
构造函数需要 byte
类型的操作数而不是浮点数。