Advertisement
evelynshilosky

DayNightSystem - Part 36

Mar 21st, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 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;
  13.     float currentTimeOfDay = 0.35f; // 8 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.  
  24.     // Update is called once per frame
  25.     void Update()
  26.     {
  27.         // Calculate the current time of day based on the game time
  28.         currentTimeOfDay += Time.deltaTime / dayDurationInSeconds;
  29.         currentTimeOfDay %= 1; // Ensure it stays between 0 and 1;
  30.  
  31.         currentHour = Mathf.FloorToInt(currentTimeOfDay * 24);
  32.  
  33.         timeUI.text = $"{currentHour}:00";
  34.  
  35.  
  36.         // Update the directional light's rotation
  37.         directionalLight.transform.rotation = Quaternion.Euler(new Vector3((currentTimeOfDay * 360) - 90, 170, 0));
  38.  
  39.         // Update the skybox material based on the time of day
  40.         UpdateSkybox();
  41.     }
  42.  
  43.     private void UpdateSkybox()
  44.     {
  45.         // Find the appropriate skybox material for the current hour
  46.         Material currentSkybox = null;
  47.         foreach (SkyboxTimeMapping mapping in timeMappings)
  48.         {
  49.             if (currentHour == mapping.hour)
  50.             {
  51.                 currentSkybox = mapping.skyboxMaterial;
  52.  
  53.                 if (currentSkybox.shader != null)
  54.                 {
  55.                     if (currentSkybox.shader.name == "Custom/SkyboxTransition")
  56.                     {
  57.                         blendedValue += Time.deltaTime;
  58.                         blendedValue = Mathf.Clamp01(blendedValue);
  59.  
  60.                         currentSkybox.SetFloat("_TransitionFactor", blendedValue);
  61.                     }
  62.                     else
  63.                     {
  64.                         blendedValue = 0;
  65.                     }
  66.                 }
  67.  
  68.                 break;
  69.             }
  70.         }
  71.          
  72.         if (currentHour == 0 && lockNextDayTrigger == false)
  73.         {
  74.             TimeManager.Instance.TriggerNextDay();
  75.             lockNextDayTrigger = true;
  76.         }
  77.  
  78.         if (currentHour != 0)
  79.         {
  80.             lockNextDayTrigger = false;
  81.         }
  82.  
  83.  
  84.         if (currentSkybox != null)
  85.         {
  86.             RenderSettings.skybox = currentSkybox;
  87.         }
  88.     }
  89. }
  90.  
  91. [System.Serializable]
  92. public class SkyboxTimeMapping
  93. {
  94.     public string phaseName;
  95.     public int hour; // The hour of the day (0-23)
  96.     public Material skyboxMaterial; // The corresponding skybox material for this hour;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement