| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public enum RotationAxis
- {
- X, Y
- }
- public class DayTimeManager : MonoBehaviour {
- public Light sunlight;
- public float secondsPerDay;
- public RotationAxis rotAxis = RotationAxis.X;
- public float maxAngle = 50.0f;
- private float _elapsedTime;
- public float timeFactor {
- get {
- return _elapsedTime / secondsPerDay;
- }
- }
- public DayTimeLabel dayTimeLabel {
- get {
- if (timeFactor >= 6.0f / 24.0f && timeFactor < 11.0f / 24.0f)
- {
- return DayTimeLabel.Morning;
- }
- if (timeFactor >= 11.0f / 24.0f && timeFactor < 17.0f / 24.0f)
- {
- return DayTimeLabel.Noon;
- }
- if (timeFactor >= 17.0f / 24.0f && timeFactor < 22.0f / 24.0f)
- {
- return DayTimeLabel.Evening;
- }
- if (timeFactor >= 23.0f / 24.0f && timeFactor < 24.0f / 24.0f)
- {
- return DayTimeLabel.Night;
- }
- if (timeFactor >= 0.0f / 24.0f && timeFactor < 6.0f / 24.0f)
- {
- return DayTimeLabel.Night;
- }
- return DayTimeLabel.Morning;
- }
- }
- private float _timeDirection = 1.0f;
- private Vector3 _position;
- private Vector3 _rotation;
- void Awake () {
- }
- // Use this for initialization
- void Start () {
- _position = sunlight.gameObject.transform.position;
- _rotation = sunlight.gameObject.transform.rotation.ToEulerAngles();
- }
-
- // Update is called once per frame
- void Update () {
-
- _elapsedTime += Time.deltaTime;
- if (_elapsedTime >= secondsPerDay)
- {
- _elapsedTime = 0;
- }
- float intensityAngle = this.timeFactor * Mathf.PI;
- sunlight.intensity = Mathf.Sin(intensityAngle);
- float rotationDirection = Mathf.Sign(this.timeFactor - 0.5f);
- switch (rotAxis)
- {
- case RotationAxis.X:
- {
- sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x + rotationDirection * maxAngle, _rotation.y, _rotation.z);
- } break;
- case RotationAxis.Y:
- {
- sunlight.gameObject.transform.rotation.SetEulerAngles(_rotation.x, _rotation.y + rotationDirection * maxAngle, _rotation.z);
- } break;
- }
- }
- float FactorToDayTime(float factor)
- {
- return factor * 24.0f;
- }
- float DayTimeToFactor(float dayTime)
- {
- return dayTime / 24.0f;
- }
- }
|