|
| 1 | +using System.Collections; |
| 2 | +using System.Collections.Generic; |
| 3 | +using UnityEngine; |
| 4 | + |
| 5 | +public class DayAndNight : MonoBehaviour |
| 6 | +{ |
| 7 | + [Range(0, 1)] |
| 8 | + public float time; |
| 9 | + public float fullDayLength; |
| 10 | + public float startTime = 0.4f; |
| 11 | + public float timeRate; |
| 12 | + public Vector3 noon; |
| 13 | + |
| 14 | + [Header("Sun")] |
| 15 | + public Light sun; |
| 16 | + public Gradient sunColor; |
| 17 | + public AnimationCurve sunIntensity; |
| 18 | + |
| 19 | + [Header("Moon")] |
| 20 | + public Light moon; |
| 21 | + public Gradient moonColor; |
| 22 | + public AnimationCurve moonIntensity; |
| 23 | + |
| 24 | + [Header("Other Seetings")] |
| 25 | + public AnimationCurve lightingIntensityMultiplier; |
| 26 | + public AnimationCurve reflectionIntensityMultiplier; |
| 27 | + |
| 28 | + public Material day; |
| 29 | + public Material night; |
| 30 | + |
| 31 | + float duration = 2.0f; |
| 32 | + |
| 33 | + |
| 34 | + // Start is called before the first frame update |
| 35 | + void Start() |
| 36 | + { |
| 37 | + timeRate = 1 / fullDayLength; |
| 38 | + time = startTime; |
| 39 | + } |
| 40 | + |
| 41 | + // Update is called once per frame |
| 42 | + void Update() |
| 43 | + { |
| 44 | + time += timeRate * Time.deltaTime; |
| 45 | + |
| 46 | + if (time >= 1) |
| 47 | + time = 0; |
| 48 | + |
| 49 | + sun.transform.eulerAngles = (time - 0.25f) * noon * 4.0f; |
| 50 | + moon.transform.eulerAngles = (time - 0.75f) * noon * 4.0f; |
| 51 | + |
| 52 | + sun.intensity = sunIntensity.Evaluate(time); |
| 53 | + moon.intensity = moonIntensity.Evaluate(time); |
| 54 | + |
| 55 | + sun.color = sunColor.Evaluate(time); |
| 56 | + moon.color = moonColor.Evaluate(time); |
| 57 | + |
| 58 | + if(sun.intensity == 0 && sun.gameObject.activeInHierarchy) |
| 59 | + { |
| 60 | + sun.gameObject.SetActive(false); |
| 61 | + } |
| 62 | + else if(sun.intensity > 0 && !sun.gameObject.activeInHierarchy) |
| 63 | + { |
| 64 | + sun.gameObject.SetActive(true); |
| 65 | + } |
| 66 | + |
| 67 | + if (moon.intensity == 0 && moon.gameObject.activeInHierarchy) |
| 68 | + { |
| 69 | + moon.gameObject.SetActive(false); |
| 70 | + } |
| 71 | + else if (moon.intensity > 0 && !moon.gameObject.activeInHierarchy) |
| 72 | + { |
| 73 | + moon.gameObject.SetActive(true); |
| 74 | + } |
| 75 | + |
| 76 | + RenderSettings.ambientIntensity = lightingIntensityMultiplier.Evaluate(time); |
| 77 | + RenderSettings.reflectionIntensity = reflectionIntensityMultiplier.Evaluate(time); |
| 78 | + } |
| 79 | +} |
0 commit comments