DayToNight.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // new GameObject("FPSTester").AddComponent<FPSTester>();
  19. }
  20. public void EnterDay()
  21. {
  22. RenderSettings.fogStartDistance = 25;
  23. RenderSettings.fogColor = new Color(124f/255, 177f/255, 221f/255);
  24. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
  25. RenderSettings.ambientSkyColor = Color.white;
  26. RenderSettings.skybox = daySkyboxMaterial;
  27. foreach (Transform item in transform) item.gameObject.SetActive(false);
  28. transform.Find("DayElements").gameObject.SetActive(true);
  29. // SetFogCamera(false);
  30. }
  31. public void EnterNight()
  32. {
  33. RenderSettings.fogStartDistance = 5;
  34. RenderSettings.fogColor = new Color(37f/255, 52f/255, 63f/255);
  35. RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
  36. RenderSettings.ambientSkyColor = new Color(160f/255, 90f/255, 90f/255);
  37. RenderSettings.skybox = nightSkyboxMaterial;
  38. foreach (Transform item in transform) item.gameObject.SetActive(false);
  39. transform.Find("NightElements").gameObject.SetActive(true);
  40. // transform.Find("NightElements/Cloud").gameObject.SetActive(false);
  41. // SetFogCamera(true);
  42. }
  43. public void SetFogCamera(bool setValue)
  44. {
  45. Component[] components = {
  46. cameras[0].GetComponent<VignetteAndChromaticAberration>(),
  47. cameras[0].GetComponent<ScreenSpaceAmbientObscurance>(),
  48. cameras[0].GetComponent<GlobalFog>()
  49. };
  50. foreach (var comp in components) {
  51. for (int i = 1; i < cameras.Length; i++) {
  52. Camera camera = cameras[i];
  53. CopyComponent(comp, camera.gameObject);
  54. }
  55. }
  56. foreach (var item in cameras)
  57. {
  58. item.GetComponent<VignetteAndChromaticAberration>().enabled = setValue;
  59. item.GetComponent<ScreenSpaceAmbientObscurance>().enabled = setValue;
  60. item.GetComponent<GlobalFog>().enabled = setValue;
  61. }
  62. }
  63. void CopyComponent(Component comp, GameObject dest)
  64. {
  65. System.Type type = comp.GetType();
  66. Component copy = dest.GetComponent(type);
  67. if (copy == null) {
  68. copy = dest.AddComponent(type);
  69. }
  70. System.Reflection.FieldInfo[] fields = type.GetFields();
  71. foreach (System.Reflection.FieldInfo field in fields)
  72. {
  73. field.SetValue(copy, field.GetValue(comp));
  74. }
  75. }
  76. }