DayToNight.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 (ChallengeGameMode.IsChallengeWolf()) {
  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. transform.Find("NightElements/Cloud").gameObject.SetActive(false);
  40. // SetFogCamera(true);
  41. }
  42. public void SetFogCamera(bool setValue)
  43. {
  44. Component[] components = {
  45. cameras[0].GetComponent<VignetteAndChromaticAberration>(),
  46. cameras[0].GetComponent<ScreenSpaceAmbientObscurance>(),
  47. cameras[0].GetComponent<GlobalFog>()
  48. };
  49. foreach (var comp in components) {
  50. for (int i = 1; i < cameras.Length; i++) {
  51. Camera camera = cameras[i];
  52. CopyComponent(comp, camera.gameObject);
  53. }
  54. }
  55. foreach (var item in cameras)
  56. {
  57. item.GetComponent<VignetteAndChromaticAberration>().enabled = setValue;
  58. item.GetComponent<ScreenSpaceAmbientObscurance>().enabled = setValue;
  59. item.GetComponent<GlobalFog>().enabled = setValue;
  60. }
  61. }
  62. void CopyComponent(Component comp, GameObject dest)
  63. {
  64. System.Type type = comp.GetType();
  65. Component copy = dest.GetComponent(type);
  66. if (copy == null) {
  67. copy = dest.AddComponent(type);
  68. }
  69. System.Reflection.FieldInfo[] fields = type.GetFields();
  70. foreach (System.Reflection.FieldInfo field in fields)
  71. {
  72. field.SetValue(copy, field.GetValue(comp));
  73. }
  74. }
  75. }