- Home /
(C#)Changing Camera Background Via Code
Hello Community,
Ill just cut to the chase. I am a little busy right now.I have a problem with my code. I am trying to make a Day and Night cycle by just changing the background of the camera. I have never done this before so that is probably why this does not work. But anyways the problem that is happening is that the colors do not blend together like they do in the unity example. And that is what I am trying to do. It still changes the color but only when it hits the certain time and it does not blend.
Here is my code
using UnityEngine;
using System.Collections;
//This script is attached to the GameManager and it controls the time of the game and the day/night
public class DayNightCycle : MonoBehaviour
{
//GameObjects
public GameObject TimeOfDayText;
public GameObject PlayerCamera;
//Colors
public Color color1 = Color.black;
public Color color2 = Color.blue;
public Color color3 = Color.blue;//This is a lighter blue
public Color color4 = Color.blue;//This is a darker blue
//Time
private float MinTime = 0f;
private float MaxTime = 2400f;
private float CurrentTime;
public float Duration1 = 3;
public float Duration2 = 3;
public float Duration3 = 3;
public float Duration4 = 3;
//Time of Day Times
public float DawnTime = 600f;
private float NoonTime = 1200f;
private float DuskTime = 2000f;
private float MidnightTime = 2400f;
//Bool for Time of day
public bool Dawn;
public bool Noon;
public bool Dusk;
public bool Midnight;
// Use this for initialization
void Start ()
{
StartCoroutine(GameTimer());
}
// Update is called once per frame
void Update ()
{
if(Dawn)
{
CurrentTime = DawnTime;
Dawn = false;
}
if(Noon)
{
CurrentTime = NoonTime;
Noon = false;
}
if(Dusk)
{
CurrentTime = DuskTime;
Dusk = false;
}
if(Midnight)
{
CurrentTime = MidnightTime;
Midnight = false;
}
AddjustCurrentTime(0);
if(CurrentTime > MidnightTime && CurrentTime <=DawnTime)//Transition from Midnight to Dawn
{
float time = Mathf.PingPong(Time.time, Duration1) / Duration1;
PlayerCamera.camera.backgroundColor = Color.Lerp(color1, color2, time);
}
if(CurrentTime > DawnTime && CurrentTime <= NoonTime)//Transition from Dawn to Noon
{
float time = Mathf.PingPong(Time.time, Duration2) / Duration2;
PlayerCamera.camera.backgroundColor = Color.Lerp(color2, color3, time);
}
if(CurrentTime > NoonTime && CurrentTime <= DuskTime)//Transition from Noon to Dusk
{
float time = Mathf.PingPong(Time.time, Duration3) / Duration3;
PlayerCamera.camera.backgroundColor = Color.Lerp(color3, color4, time);
}
if(CurrentTime > DuskTime && CurrentTime <= MidnightTime)//Transition from Dusk to Midnight
{
float time = Mathf.PingPong(Time.time, Duration4) / Duration4;
PlayerCamera.camera.backgroundColor = Color.Lerp(color4, color1, time);
}
}
void OnGUI()
{
TimeOfDayText.GetComponent<TextMesh>().text = "It is " + CurrentTime;
}
void CameraFlags()
{
camera.clearFlags = CameraClearFlags.SolidColor;
}
public void AddjustCurrentTime(int adj)
{
CurrentTime += adj;
if(CurrentTime < MinTime)
{
CurrentTime = MinTime;
}
if(CurrentTime >= MaxTime)
{
CurrentTime = MinTime;
}
}
IEnumerator GameTimer()
{
while(true)
{
if(CurrentTime < MaxTime)
{
CurrentTime += 1f;
yield return new WaitForSeconds(1f);
}
else
{
yield return null;
}
}
}
}
but these lines of code do not work for me, they are in red. Its like unity 5 doesnt support camera components any more:( can you please help
PlayerCamera.camera.clearFlags(red)
$$anonymous$$yCam.camera.backgroundColor(red)
Answer by Infinite_Gamer · Apr 22, 2014 at 02:30 AM
OK sorry for answering my own question but What I ended up doing was just starting over and trying a little different way of doing it and it is working fine.
Here is my fixed code
using UnityEngine;
using System.Collections;
public class DayNightCycle : MonoBehaviour
//Somewhat working
//The duration is messed up so I think that is the problem
{
//Get GameObjects
public GameObject TimeOfDayText;
public GameObject PlayerCamera;
//Sky Colors
public Color Black = Color.black;
public Color Blue = Color.blue;
public Color LightBlue = Color.blue;
public Color DarkBlue = Color.blue;
//Durations
public float Duration1 = 60f;
public float Duration2 = 800f;
public float Duration3 = 400f;
//Bools
private bool Dawn;
private bool Noon;
private bool Dusk;
private bool Midnight;
//Time
private float MinTime = 2f;
private float MaxTime = 2400f;
public float CurrentTime;
// Use this for initialization
void Start ()
{
StartCoroutine(GameTimer());
}
// Update is called once per frame
void Update ()
{
//Dawn
if(CurrentTime > 0 && CurrentTime <= 600)
{
Dawn = true;
}
else
{
Dawn = false;
}
//Noon
if(CurrentTime > 600 && CurrentTime <= 1200)
{
Noon = true;
}
else
{
Noon = false;
}
//Dusk
if(CurrentTime > 1200 && CurrentTime <= 2000)
{
Dusk = true;
}
else
{
Dusk = false;
}
//Midnight
if(CurrentTime > 2000 && CurrentTime <= 2400)
{
Midnight = true;
}
else
{
Midnight = false;
}
AddjustCurrentTime(0);
if(Dawn)
{
float t = Mathf.PingPong(Time.time, Duration1) / Duration1;
PlayerCamera.camera.backgroundColor = Color.Lerp(Black, Blue, t);
}
if(Noon)
{
float t = Mathf.PingPong(Time.time, Duration1) / Duration1;
PlayerCamera.camera.backgroundColor = Color.Lerp(Blue, LightBlue, t);
}
if(Dusk)
{
float t = Mathf.PingPong(Time.time, Duration2) / Duration2;
PlayerCamera.camera.backgroundColor = Color.Lerp(LightBlue, DarkBlue, t);
}
if(Midnight)
{
float t = Mathf.PingPong(Time.time, Duration3) / Duration3;
camera.backgroundColor = Color.Lerp(DarkBlue, Black, t);
}
}
void CameraFlags()
{
PlayerCamera.camera.clearFlags = CameraClearFlags.SolidColor;
}
void OnGUI()
{
TimeOfDayText.GetComponent<TextMesh>().text = "It is " + CurrentTime;
}
public void AddjustCurrentTime(int adj)
{
CurrentTime += adj;
if(CurrentTime < MinTime)
{
CurrentTime = MinTime;
}
if(CurrentTime >= MaxTime)
{
CurrentTime = MinTime;
}
}
IEnumerator GameTimer()
{
while(true)
{
if(CurrentTime < MaxTime)
{
CurrentTime += 1f;
yield return new WaitForSeconds(1f);
}
else
{
yield return null;
}
}
}
}
Hey,
I was expressly glad to have answered your own question as I face a similar issue. Thanks for this easy and great way to implement Day&Night cycle.
Not so long ago I've needed a solution to handle Onvif cameras. The hard part was to stream to multiple remote locations after the working hours (in the afternoon and at night). After some googling I've found an SD$$anonymous$$ that enables to build an Onvif video server in C#. It made it possible to display the IP camera image on remote laptops and Android phones using RTSP strea$$anonymous$$g. The point is that I can integrate your Day&Night feature superbly to my video server application.
Thanks again!
Have a nice day
Your answer
Follow this Question
Related Questions
I'm having trouble with Color.Lerp() 0 Answers
Multiple Cars not working 1 Answer
Change the background color attribute of a camera in C#? 2 Answers
Creation of the background in Alto's adventure game 1 Answer
Distribute terrain in zones 3 Answers