DayToNight.cs 2.7 KB

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