mirror of
				https://github.com/DerTyp7/fps-citybuild-unity.git
				synced 2025-10-31 05:07:08 +01:00 
			
		
		
		
	Skybox, Day Night Cycle, Better TimeManager
This commit is contained in:
		
							
								
								
									
										74
									
								
								Assets/Scripts/LightingManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								Assets/Scripts/LightingManager.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | ||||
| using UnityEngine; | ||||
|  | ||||
| [ExecuteAlways] | ||||
| public class LightingManager : MonoBehaviour | ||||
| { | ||||
|     //Scene References | ||||
|     [SerializeField] private Light DirectionalLight; | ||||
|     [SerializeField] private LightingPreset Preset; | ||||
|     [SerializeField] private GameObject GamerManager; | ||||
|     //Variables | ||||
|     [SerializeField, Range(0, 24)] private float TimeOfDay; | ||||
|  | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         if (Preset == null) | ||||
|             return; | ||||
|  | ||||
|         if (Application.isPlaying) | ||||
|         { | ||||
|             //(Replace with a reference to the game time) | ||||
|             TimeOfDay = GamerManager.GetComponent<TimeManager>().GetTimeOfDayFloat(); | ||||
|             TimeOfDay %= 24; //Modulus to ensure always between 0-24 | ||||
|             UpdateLighting(TimeOfDay / 24f); | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             UpdateLighting(TimeOfDay / 24f); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     private void UpdateLighting(float timePercent) | ||||
|     { | ||||
|         //Set ambient and fog | ||||
|         RenderSettings.ambientLight = Preset.AmbientColor.Evaluate(timePercent); | ||||
|         RenderSettings.fogColor = Preset.FogColor.Evaluate(timePercent); | ||||
|  | ||||
|         //If the directional light is set then rotate and set it's color, I actually rarely use the rotation because it casts tall shadows unless you clamp the value | ||||
|         if (DirectionalLight != null) | ||||
|         { | ||||
|             DirectionalLight.color = Preset.DirectionalColor.Evaluate(timePercent); | ||||
|  | ||||
|             DirectionalLight.transform.localRotation = Quaternion.Euler(new Vector3((timePercent * 360f) - 90f, 170f, 0)); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     //Try to find a directional light to use if we haven't set one | ||||
|     private void OnValidate() | ||||
|     { | ||||
|         if (DirectionalLight != null) | ||||
|             return; | ||||
|  | ||||
|         //Search for lighting tab sun | ||||
|         if (RenderSettings.sun != null) | ||||
|         { | ||||
|             DirectionalLight = RenderSettings.sun; | ||||
|         } | ||||
|         //Search scene for light that fits criteria (directional) | ||||
|         else | ||||
|         { | ||||
|             Light[] lights = GameObject.FindObjectsOfType<Light>(); | ||||
|             foreach (Light light in lights) | ||||
|             { | ||||
|                 if (light.type == LightType.Directional) | ||||
|                 { | ||||
|                     DirectionalLight = light; | ||||
|                     return; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 DerTyp187
					DerTyp187