Advertisement
evelynshilosky

DayNightSystem

Apr 26th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.51 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6.  
  7. public class DayNightSystem : MonoBehaviour
  8. {
  9.     public Light directionalLight;
  10.  
  11.     public float dayDurationInSeconds = 24.0f; // Adjust the duration of a full day in seconds
  12.     public int currentHour; private int seconds = 0;
  13.     float currentTimeOfDay = 0.35f;   float currentTimeOfDayForSeconds = 0f; // 8:00 AM
  14.  
  15.     public List<SkyboxTimeMapping> timeMappings;
  16.    
  17.     float blendedValue = 0.0f;
  18.  
  19.     bool lockNextDayTrigger = false;
  20.  
  21.     public TextMeshProUGUI timeUI;
  22.  
  23.     public WeatherSystem weatherSystem;
  24.  
  25.  
  26.     // Update is called once per frame
  27.     void Update()
  28.     {
  29.         // Calculate the current time of day based on the game time
  30.         currentTimeOfDay += Time.deltaTime / dayDurationInSeconds; currentTimeOfDayForSeconds += Time.deltaTime / (dayDurationInSeconds / 40);
  31.         currentTimeOfDay %= 1; currentTimeOfDayForSeconds %= 1; // Ensure it stays between 0 and 1;
  32.  
  33.         currentHour = Mathf.FloorToInt(currentTimeOfDay * 24); seconds = Mathf.FloorToInt(currentTimeOfDayForSeconds * 59);
  34.        
  35.         if (seconds < 10) { if (currentHour > 12) { timeUI.text = $"{currentHour-11}:0{seconds}"; } else { timeUI.text = $"{currentHour}:0{seconds}"; }  }       else        {            if (currentHour > 12)            {                timeUI.text = $"{currentHour - 11}:{seconds}";            }            else            {                timeUI.text = $"{currentHour}:{seconds}";            }        }
  36.         //timeUI.text = $"{currentHour}:00"; Replaced with below code ^
  37.  
  38.         // Update the directional light's rotation
  39.         directionalLight.transform.rotation = Quaternion.Euler(new Vector3((currentTimeOfDay * 360) - 90, 170, 0));
  40.  
  41.  
  42.         // Update the skybox material based on the time of day
  43.         if (weatherSystem.isSpecialWeather == false)
  44.         {
  45.             UpdateSkybox();
  46.         }
  47.  
  48.         if (currentHour == 0 && lockNextDayTrigger == false)
  49.         {
  50.             TimeManager.Instance.TriggerNextDay();
  51.             lockNextDayTrigger = true;
  52.         }
  53.  
  54.         if (currentHour != 0)
  55.         {
  56.             lockNextDayTrigger = false;
  57.         }
  58.  
  59.  
  60.     }
  61.  
  62.     private void UpdateSkybox()
  63.     {
  64.         // Find the appropriate skybox material for the current hour
  65.         Material currentSkybox = null;
  66.         foreach (SkyboxTimeMapping mapping in timeMappings)
  67.         {
  68.             if (currentHour == mapping.hour)
  69.             {
  70.                 currentSkybox = mapping.skyboxMaterial;
  71.  
  72.                 if (currentSkybox.shader != null)
  73.                 {
  74.                     if (currentSkybox.shader.name == "Custom/SkyboxTransition")
  75.                     {
  76.                         blendedValue += Time.deltaTime;
  77.                         blendedValue = Mathf.Clamp01(blendedValue);
  78.  
  79.                         currentSkybox.SetFloat("_TransitionFactor", blendedValue);
  80.                     }
  81.                     else
  82.                     {
  83.                         blendedValue = 0;
  84.                     }
  85.                 }
  86.  
  87.                 break;
  88.             }
  89.         }
  90.          
  91.        
  92.  
  93.  
  94.         if (currentSkybox != null)
  95.         {
  96.             RenderSettings.skybox = currentSkybox;
  97.         }
  98.     }
  99. }
  100.  
  101. [System.Serializable]
  102. public class SkyboxTimeMapping
  103. {
  104.     public string phaseName;
  105.     public int hour; // The hour of the day (0-23)
  106.     public Material skyboxMaterial; // The corresponding skybox material for this hour;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement