[ExecuteAlways]
public class LightingManager : MonoBehaviour
{
//Scene References
[SerializeField] private Light DirectionalLight;
[SerializeField] private DayNight Preset;
[SerializeField] private float _targetDayLength = 0.5f;
[SerializeField] private Material[] morningSky;
[SerializeField] private Material[] AfternoonSky;
[SerializeField] private Material[] NightSky;
[SerializeField] private AudioClip[] dayAudio;
[SerializeField] private AudioClip[] nightAudio;
//Variables
[SerializeField][Range(0f, 1f)] private float TimeOfDay;
private float _timeScale = 100f;
[SerializeField]
private float elapsedTime;
[SerializeField] private int dayNumber;
[SerializeField] private float yearLength = 100;
private int yearNumber;
private bool isMorning = false;
private bool isNight = false;
private bool isAfternoon = false;
[SerializeField] private AudioSource daySource;
[SerializeField] private AudioSource nightSource;
private void Start()
{
daySource = GetComponent<AudioSource>();
}
private void Update()
{
if(TimeOfDay >= 0.2f && TimeOfDay <= 0.5f && isMorning == false)
{
RenderSettings.skybox = morningSky[Random.Range(0, morningSky.Length)];
daySource.Play();
nightSource.Stop();
DirectionalLight.intensity = 0.6f;
isMorning = true;
isNight = false;
isAfternoon = false;
}
if(TimeOfDay>= 0.6f && TimeOfDay <= 0.8f && isAfternoon == false)
{
RenderSettings.skybox = AfternoonSky[Random.Range(0, AfternoonSky.Length)];
DirectionalLight.intensity = 1.0f;
isMorning = false;
isAfternoon = true;
isNight = false;
}
if(TimeOfDay >= 0.8f && isNight == false)
{
RenderSettings.skybox = NightSky[Random.Range(0, NightSky.Length)];
isAfternoon = false;
isNight = true;
DirectionalLight.intensity = 0.2f;
daySource.Stop();
nightSource.Play();
}
if (Preset == null)
return;
if (Application.isPlaying)
{
//(Replace with a reference to the game time)
UpdateTime();//Modulus to ensure always between 0-24
UpdateLighting(TimeOfDay);
UpdateTimeScale();
}
else
{
UpdateLighting(TimeOfDay);
}
}
private void UpdateTimeScale()
{
_timeScale = 24 / (_targetDayLength / 60);
}
private void UpdateTime()
{
TimeOfDay += Time.deltaTime * _timeScale / 86400; // seconds in a day
elapsedTime += Time.deltaTime;
if (TimeOfDay > 1) //new day!!
{
elapsedTime = 0;
dayNumber++;
TimeOfDay -= 1;
if (dayNumber > yearLength) //new year!
{
yearNumber++;
dayNumber = 0;
}
}
}
private void UpdateLighting(float timePercent)
{
//Set ambient and fog
RenderSettings.ambientLight = Preset.AmbientColor.Evaluate(timePercent);
RenderSettings.fogColor = Preset.FogColor.Evaluate(timePercent);
//If the directional light is set then rotate and set it's color, I actually rarely use the rotation because it casts tall shadows unless you clamp the value
if (DirectionalLight != null)
{
DirectionalLight.color = Preset.DirectionalColor.Evaluate(timePercent);
DirectionalLight.transform.localRotation = Quaternion.Euler(new Vector3((timePercent * 360f) - 90f, 170f, 0));
}
}
//Try to find a directional light to use if we haven't set one
private void OnValidate()
{
if (DirectionalLight != null)
return;
//Search for lighting tab sun
if (RenderSettings.sun != null)
{
DirectionalLight = RenderSettings.sun;
}
//Search scene for light that fits criteria (directional)
else
{
Light[] lights = GameObject.FindObjectsOfType<Light>();
foreach (Light light in lights)
{
if (light.type == LightType.Directional)
{
DirectionalLight = light;
return;
}
}
}
}
}
Hi,
In which course and lecture are you?