简体   繁体   中英

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;

But how to change it after every 10 sec interval, is there any timer like something which can be called after certain time.

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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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