HomeSunLight.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Experimental.Rendering;
  5. using UnityEngine.Rendering;
  6. /* 本文件控制家园场景的光照设置
  7. * 1. 通过时间来设置光照
  8. * 2. 通过狗被唤醒来设置光照
  9. * 3. 通过时间来设置天空盒
  10. * 4. 通过时间来设置BGM音量
  11. */
  12. public class HomeSunLight : MonoBehaviour
  13. {
  14. // Start is called before the first frame update
  15. public static HomeSunLight Instance;
  16. public Light mainLight;
  17. public GameObject targetCamera;
  18. public Material daySkybox;
  19. public Material nightSkybox;
  20. void Awake()
  21. {
  22. // 单例模式,确保只有一个GlobalSoundManager
  23. if (Instance == null)
  24. {
  25. Instance = this;
  26. //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行
  27. }
  28. else
  29. {
  30. Destroy(gameObject);
  31. }
  32. }
  33. void Start()
  34. {
  35. GameObject homeLight = GameObject.Find("Home Light");
  36. int hour = System.DateTime.Now.Hour;
  37. if ( hour >=22 || hour < 5) // 深夜时间
  38. {
  39. MidNightLightSetting();
  40. }
  41. else if (hour < 6 || hour >= 18)
  42. {
  43. NightLightSetting();
  44. }
  45. else
  46. {
  47. DayLightSetting();
  48. }
  49. DynamicGI.UpdateEnvironment();
  50. }
  51. // Update is called once per frame
  52. //void Update()
  53. //{
  54. //}
  55. void DayLightSetting()
  56. {
  57. Debug.Log("day time");
  58. // day time
  59. mainLight.intensity = 0.9f;
  60. mainLight.range = 100f;
  61. //targetCamera.GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;
  62. //targetCamera.GetComponent<Camera>().backgroundColor = new Color(139, 202, 255, 203);
  63. UnityEngine.RenderSettings.ambientMode = AmbientMode.Flat;
  64. UnityEngine.RenderSettings.ambientLight = new Color32(200, 200, 200, 180);
  65. UnityEngine.RenderSettings.skybox = daySkybox;
  66. UnityEngine.RenderSettings.ambientIntensity = 1f;
  67. UnityEngine.RenderSettings.reflectionIntensity = 0.5f;
  68. var BGM = GameObject.Find("BGM");
  69. if (BGM != null)
  70. {
  71. BGM.GetComponent<AudioSource>().volume = 0.4f;
  72. }
  73. }
  74. void NightLightSetting()
  75. {
  76. // night time
  77. mainLight.intensity = 0.6f;
  78. mainLight.range = 100f;
  79. //targetCamera.GetComponent<Camera>().backgroundColor = new Color(22, 42, 113, 239);
  80. UnityEngine.RenderSettings.skybox = nightSkybox;
  81. UnityEngine.RenderSettings.ambientIntensity = 0.2f;
  82. UnityEngine.RenderSettings.reflectionIntensity = 0.2f;
  83. var BGM = GameObject.Find("BGM");
  84. if (BGM != null)
  85. {
  86. BGM.GetComponent<AudioSource>().volume = 0.4f;
  87. }
  88. }
  89. void MidNightLightSetting()
  90. {
  91. mainLight.intensity = 0.1f;
  92. //targetCamera.GetComponent<Camera>().backgroundColor = new Color(22, 42, 113, 239);
  93. mainLight.range = 5f;
  94. UnityEngine.RenderSettings.skybox = nightSkybox;
  95. UnityEngine.RenderSettings.ambientIntensity = 0.2f;
  96. UnityEngine.RenderSettings.reflectionIntensity = 0.2f;
  97. var BGM = GameObject.Find("BGM");
  98. if (BGM != null)
  99. {
  100. BGM.GetComponent<AudioSource>().volume = 0.1f;
  101. }
  102. }
  103. // 深夜模式当狗被唤醒时候切换显示亮度
  104. public void DogWakeUpLightSetting()
  105. {
  106. int hour = System.DateTime.Now.Hour;
  107. if (hour >= 22 || hour < 5) // 深夜时间
  108. {
  109. HomeSunLight.Instance.NightLightSetting();
  110. }
  111. }
  112. }