TimeManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections;
  3. using TMPro;
  4. using UnityEngine;
  5. public class TimeManager : MonoBehaviour
  6. {
  7. public float gameSpeed = 1f;
  8. public Texture2D skyboxNight;
  9. public Texture2D skyboxSunrise;
  10. public Texture2D skyboxDay;
  11. public Texture2D skyboxSunset;
  12. public Gradient gradientNightToSunrise;
  13. public Gradient gradientSunriseToDay;
  14. public Gradient gradientDayToSunset;
  15. public Gradient gradientSunsetToNight;
  16. public Light globalLight;
  17. public int minutes;
  18. public int Minutes { get { return minutes; } set { minutes = value; OnMinutesChanged(value); } }
  19. public int hours;
  20. public int Hours { get { return hours; } set { hours = value; OnHoursChanged(value); } }
  21. public int days;
  22. public int Days { get { return days; } set { days = value; } }
  23. public float tempSecond;
  24. public TMP_Text TimeField;
  25. // Start is called once before the first execution of Update after the MonoBehaviour is created
  26. void Start()
  27. {
  28. UpdateUI();
  29. }
  30. // Update is called once per frame
  31. void Update()
  32. {
  33. tempSecond += Time.deltaTime * gameSpeed;
  34. if(tempSecond >= 1)
  35. {
  36. Minutes++;
  37. tempSecond--;
  38. }
  39. }
  40. private void UpdateUI()
  41. {
  42. TimeField.text = Hours.ToString("D2") + " : " + Minutes.ToString("D2");
  43. }
  44. private void OnMinutesChanged(float value)
  45. {
  46. globalLight.transform.Rotate(Vector3.up, (1f / 1440f) * 360f, Space.World);
  47. if(value >= 60)
  48. {
  49. Hours++;
  50. Minutes -= 60;
  51. }
  52. if (Hours >= 24)
  53. {
  54. Hours -= 24;
  55. Days++;
  56. }
  57. UpdateUI();
  58. }
  59. private void OnHoursChanged(float value)
  60. {
  61. switch (value)
  62. {
  63. case 6:
  64. StartCoroutine(LerpSkybox(skyboxNight, skyboxSunrise, 10f));
  65. StartCoroutine(LerpLight(gradientNightToSunrise, 10f));
  66. break;
  67. case 10:
  68. StartCoroutine(LerpSkybox(skyboxSunrise, skyboxDay, 10f));
  69. StartCoroutine(LerpLight(gradientSunriseToDay, 10f));
  70. break;
  71. case 18:
  72. StartCoroutine(LerpSkybox(skyboxDay, skyboxSunset, 10f));
  73. StartCoroutine(LerpLight(gradientDayToSunset, 10f));
  74. break;
  75. case 22:
  76. StartCoroutine(LerpSkybox(skyboxSunset, skyboxNight, 10f));
  77. StartCoroutine(LerpLight(gradientSunsetToNight, 10f));
  78. break;
  79. }
  80. }
  81. private IEnumerator LerpSkybox(Texture2D a, Texture2D b, float time)
  82. {
  83. RenderSettings.skybox.SetTexture("_Texture1", a);
  84. RenderSettings.skybox.SetTexture("_Texture2", b);
  85. RenderSettings.skybox.SetFloat("_Blend", 0);
  86. for (float i=0; i < time; i += Time.deltaTime)
  87. {
  88. RenderSettings.skybox.SetFloat("_Blend", i / time);
  89. yield return null;
  90. }
  91. RenderSettings.skybox.SetTexture("_Texture1", b);
  92. }
  93. private IEnumerator LerpLight(Gradient lightGradient, float time)
  94. {
  95. for (float i = 0; i < time; i += Time.deltaTime)
  96. {
  97. globalLight.color = lightGradient.Evaluate(i / time);
  98. RenderSettings.fogColor = globalLight.color;
  99. yield return null;
  100. }
  101. }
  102. }