DayToNight.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityStandardAssets.ImageEffects;
  6. public class DayToNight : MonoBehaviour
  7. {
  8. [SerializeField] Material daySkyboxMaterial;
  9. [SerializeField] Material nightSkyboxMaterial;
  10. [SerializeField] Camera[] cameras;
  11. void Awake()
  12. {
  13. if (GameMgr.gameType == 5) {
  14. EnterNight();
  15. } else {
  16. EnterDay();
  17. }
  18. }
  19. public void EnterDay()
  20. {
  21. RenderSettings.fogStartDistance = 25;
  22. RenderSettings.fogColor = new Color(124f/255, 177f/255, 221f/255);
  23. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
  24. RenderSettings.ambientSkyColor = Color.white;
  25. RenderSettings.skybox = daySkyboxMaterial;
  26. foreach (Transform item in transform) item.gameObject.SetActive(false);
  27. transform.Find("DayElements").gameObject.SetActive(true);
  28. SetFogCamera(false);
  29. }
  30. public void EnterNight()
  31. {
  32. RenderSettings.fogStartDistance = 5;
  33. RenderSettings.fogColor = new Color(37f/255, 52f/255, 63f/255);
  34. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
  35. RenderSettings.ambientSkyColor = new Color(160f/255, 90f/255, 90f/255);
  36. RenderSettings.skybox = nightSkyboxMaterial;
  37. foreach (Transform item in transform) item.gameObject.SetActive(false);
  38. transform.Find("NightElements").gameObject.SetActive(true);
  39. SetFogCamera(true);
  40. }
  41. public void SetFogCamera(bool setValue)
  42. {
  43. Component[] components = {
  44. cameras[0].GetComponent<VignetteAndChromaticAberration>(),
  45. cameras[0].GetComponent<ScreenSpaceAmbientObscurance>(),
  46. cameras[0].GetComponent<GlobalFog>()
  47. };
  48. foreach (var comp in components) {
  49. for (int i = 1; i < cameras.Length; i++) {
  50. Camera camera = cameras[i];
  51. CopyComponent(comp, camera.gameObject);
  52. }
  53. }
  54. foreach (var item in cameras)
  55. {
  56. item.GetComponent<VignetteAndChromaticAberration>().enabled = setValue;
  57. item.GetComponent<ScreenSpaceAmbientObscurance>().enabled = setValue;
  58. item.GetComponent<GlobalFog>().enabled = setValue;
  59. }
  60. }
  61. void CopyComponent(Component comp, GameObject dest)
  62. {
  63. System.Type type = comp.GetType();
  64. Component copy = dest.GetComponent(type);
  65. if (copy == null) {
  66. copy = dest.AddComponent(type);
  67. }
  68. System.Reflection.FieldInfo[] fields = type.GetFields();
  69. foreach (System.Reflection.FieldInfo field in fields)
  70. {
  71. field.SetValue(copy, field.GetValue(comp));
  72. }
  73. }
  74. }