更具体一点,我想快速改变颜色(比如每秒60次),我想把它从蓝色改为红色,再改为绿色,然后再改回来,重复一遍又一遍
答案 0 :(得分:1)
如果真的想要这样做,(我完全看不到它的实际应用,如javascript demo所示),以下代码将快速转换背景颜色场景(每帧一次)。
在相机属性下,将Clear Flags
更改为Solid Color
。这会禁用天空盒背景,而只是将背景清除为颜色。
然后,使用以下代码创建一个新的C#行为并将其附加到您的相机:
public class SkyColorBehaviourScript : MonoBehaviour {
// used to track the index of the background to display
public int cycleIndex = 0;
Color[] skyColors = new Color[3];
void Start () {
// init the sky colors array
skyColors [0] = new Color (255, 0, 0); // red
skyColors [1] = new Color (0, 255, 0); // green
skyColors [2] = new Color (0, 0, 255); // blue
}
// Update is called once per frame
void Update () {
// cycle the camera background color
cycleIndex++;
cycleIndex %= skyColors.Length;
camera.backgroundColor = skyColors [cycleIndex];
}
}
<强>解释强>
该脚本有一个数组skyColors
,包含三种颜色,红色,绿色和蓝色。
每次更新(每帧一次)时,变量cycleIndex会递增。
然后,通过调用cycleIndex %= skyColors.Length
,每当cycleIndex等于colors数组的长度时,它就会重置为零。 (这样,如果你向数组中添加更多颜色,它也将循环遍历它们。)
最后,我们将相机的背景颜色更改为cycleIndex索引的数组中的颜色。
默认帧速率可能会在60-100Hz左右锁定到显示器的刷新率,但如果禁用vsync,则可以将目标帧速率设置得更高。但请注意,更新只会以图形硬件可以处理的速度运行,并且如果关闭vsync,您将会遇到丑陋的“撕裂”效果。
通过Skybox着色的替代方法
如果由于某种原因您想要更改预设天空盒的色调而不是更改活动相机的清晰颜色,则可以使用此版本的更新方法:
// Update is called once per frame
void Update () {
// cycle the camera background color
cycleIndex++;
cycleIndex %= skyColors.Length;
RenderSettings.skybox.SetColor("_Tint", skyColors [cycleIndex]);
}
请注意,这假设您已经通过RenderSettings将天空盒应用于所有摄像机,而不是每个摄像机Skybox。有了这个版本,活动相机的清晰标志需要设置为天空盒,而你只需要更改天空盒的色调,因此一些天空盒纹理可能仍然可见(即它赢了' t是纯红色,蓝色和绿色背景)
警告:这两种技术都可能导致癫痫发作。