Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Infinite_Gamer · Apr 20, 2014 at 03:56 PM · c#camerabackgrounddaynight

(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;
             }
             
         }
     }
 }



 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chintan_shroff · Oct 31, 2015 at 07:20 AM 0
Share

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)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
             }
             
         }
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image notBADrian · Oct 30, 2014 at 08:42 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges