123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Experimental.Rendering;
- using UnityEngine.Rendering;
- /* 本文件控制家园场景的光照设置
- * 1. 通过时间来设置光照
- * 2. 通过狗被唤醒来设置光照
- * 3. 通过时间来设置天空盒
- * 4. 通过时间来设置BGM音量
- */
- public class HomeSunLight : MonoBehaviour
- {
- // Start is called before the first frame update
- public static HomeSunLight Instance;
- public Light mainLight;
- public GameObject targetCamera;
- public Material daySkybox;
- public Material nightSkybox;
- void Awake()
- {
- // 单例模式,确保只有一个GlobalSoundManager
- if (Instance == null)
- {
- Instance = this;
- //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行
- }
- else
- {
- Destroy(gameObject);
- }
- }
- void Start()
- {
- GameObject homeLight = GameObject.Find("Home Light");
- int hour = System.DateTime.Now.Hour;
- if ( hour >=22 || hour < 5) // 深夜时间
- {
- MidNightLightSetting();
- }
- else if (hour < 6 || hour >= 18)
- {
- NightLightSetting();
- }
- else
- {
- DayLightSetting();
- }
- DynamicGI.UpdateEnvironment();
- }
- // Update is called once per frame
- //void Update()
- //{
-
- //}
- void DayLightSetting()
- {
- Debug.Log("day time");
- // day time
- mainLight.intensity = 0.9f;
- mainLight.range = 100f;
- //targetCamera.GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;
- //targetCamera.GetComponent<Camera>().backgroundColor = new Color(139, 202, 255, 203);
- UnityEngine.RenderSettings.ambientMode = AmbientMode.Flat;
- UnityEngine.RenderSettings.ambientLight = new Color32(200, 200, 200, 180);
- UnityEngine.RenderSettings.skybox = daySkybox;
- UnityEngine.RenderSettings.ambientIntensity = 1f;
- UnityEngine.RenderSettings.reflectionIntensity = 0.5f;
- var BGM = GameObject.Find("BGM");
- if (BGM != null)
- {
- BGM.GetComponent<AudioSource>().volume = 0.4f;
- }
- }
- void NightLightSetting()
- {
- // night time
- mainLight.intensity = 0.6f;
- mainLight.range = 100f;
- //targetCamera.GetComponent<Camera>().backgroundColor = new Color(22, 42, 113, 239);
- UnityEngine.RenderSettings.skybox = nightSkybox;
- UnityEngine.RenderSettings.ambientIntensity = 0.2f;
- UnityEngine.RenderSettings.reflectionIntensity = 0.2f;
- var BGM = GameObject.Find("BGM");
- if (BGM != null)
- {
- BGM.GetComponent<AudioSource>().volume = 0.4f;
- }
- }
- void MidNightLightSetting()
- {
- mainLight.intensity = 0.1f;
- //targetCamera.GetComponent<Camera>().backgroundColor = new Color(22, 42, 113, 239);
- mainLight.range = 5f;
- UnityEngine.RenderSettings.skybox = nightSkybox;
- UnityEngine.RenderSettings.ambientIntensity = 0.2f;
- UnityEngine.RenderSettings.reflectionIntensity = 0.2f;
- var BGM = GameObject.Find("BGM");
- if (BGM != null)
- {
- BGM.GetComponent<AudioSource>().volume = 0.1f;
- }
- }
- // 深夜模式当狗被唤醒时候切换显示亮度
- public void DogWakeUpLightSetting()
- {
- int hour = System.DateTime.Now.Hour;
- if (hour >= 22 || hour < 5) // 深夜时间
- {
- HomeSunLight.Instance.NightLightSetting();
- }
- }
- }
|