简体   繁体   English

在Unity3D c#中每10秒更改一次相机颜色

[英]Change camera color after every 10 sec in Unity3D c#

I want to change the camera colour in unity and I know how to change it once in the script camera.backgroundColor = Color.red; 我想统一更改相机的颜色,我知道如何在脚本camera.backgroundColor = Color.red;中更改一次。

But how to change it after every 10 sec interval, is there any timer like something which can be called after certain time. 但是如何每隔10秒更改一次,是否有计时器可以在一定时间后调用。

Thank You 谢谢

You can use timer, 您可以使用计时器,

Timer tm = new Timer(ChangeColor, cameraObject, 0, 1000);
    private void ChangeColor(object camera)
    {
        //camera is your camera object
        if (camera != null)
        {
            camera.backgroundColor = Color.red;
        }
    }

Also you can pass the color as parameter in ChangeColor method to set the desired color. 您也可以在ChangeColor方法中将颜色作为参数传递,以设置所需的颜色。

Timer tm = new Timer(ChangeColor, color, 0, 1000);
        private void ChangeColor(object color)
        {   
            Color backColor = color as Color;
            // camera is member variable
            if (color!= null)
            {
                camera.backgroundColor = backColor ;
            }
        }

or You can also pass the camera object and color both as Tuple. 或者,您也可以传递相机对象并将颜色都设置为元组。

You could use Timer , however I would solve it with a simple condition in your Update() method: 您可以使用Timer ,但是我可以通过Update()方法中的一个简单条件来解决它:

float elapsedTime;

void Update()
{
    elapsedTime += Time.deltaTime;

    if (elapsedTime >= 10)
    {
        elapsedTime -= 10;
        // insert logic for changing color below:
        camera.backgroundColor = Color.red;
    }
}

In my opinion, this is easier to use. 我认为这更易于使用。

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

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