mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-29 20:22:08 +01:00
Skybox, Day Night Cycle, Better TimeManager
This commit is contained in:
74
Assets/Scripts/LightingManager.cs
Normal file
74
Assets/Scripts/LightingManager.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using UnityEngine;
|
||||
|
||||
[ExecuteAlways]
|
||||
public class LightingManager : MonoBehaviour
|
||||
{
|
||||
//Scene References
|
||||
[SerializeField] private Light DirectionalLight;
|
||||
[SerializeField] private LightingPreset Preset;
|
||||
[SerializeField] private GameObject GamerManager;
|
||||
//Variables
|
||||
[SerializeField, Range(0, 24)] private float TimeOfDay;
|
||||
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Preset == null)
|
||||
return;
|
||||
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
//(Replace with a reference to the game time)
|
||||
TimeOfDay = GamerManager.GetComponent<TimeManager>().GetTimeOfDayFloat();
|
||||
TimeOfDay %= 24; //Modulus to ensure always between 0-24
|
||||
UpdateLighting(TimeOfDay / 24f);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateLighting(TimeOfDay / 24f);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/LightingManager.cs.meta
Normal file
11
Assets/Scripts/LightingManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9536869875fd37e4ba83c4f675ad13a8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
10
Assets/Scripts/LightingPreset.cs
Normal file
10
Assets/Scripts/LightingPreset.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
[CreateAssetMenu(fileName = "Lighting Preset", menuName = "Scriptables/Lighting Preset", order = 1)]
|
||||
public class LightingPreset : ScriptableObject
|
||||
{
|
||||
public Gradient AmbientColor;
|
||||
public Gradient DirectionalColor;
|
||||
public Gradient FogColor;
|
||||
}
|
||||
11
Assets/Scripts/LightingPreset.cs.meta
Normal file
11
Assets/Scripts/LightingPreset.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8d88d7314c3e0844907ef188c4789f3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,129 +1,47 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
//Eine Minute geht 0.23 Sekunden
|
||||
//Die Sekunden werden gez<65>hlt
|
||||
|
||||
public class TimeManager : MonoBehaviour
|
||||
{
|
||||
[Header("TimeManager")]
|
||||
[SerializeField] private float timePeriod = 3f;
|
||||
[SerializeField] private Text timeUI;
|
||||
[SerializeField] private float timePeriod = 0.02f;
|
||||
|
||||
public int minutes = 0;
|
||||
public int hours = 0;
|
||||
public string current_time = "";
|
||||
public int secondsOfDay = 0;
|
||||
|
||||
public int day = 1;
|
||||
public int month = 1;
|
||||
public int year = -5000;
|
||||
public string yearStr = "";
|
||||
public string current_date = "";
|
||||
|
||||
private void Start()
|
||||
{
|
||||
InvokeRepeating("timeUpdate", 1f, timePeriod);
|
||||
InvokeRepeating("TimeUpdate", 1f, timePeriod);
|
||||
}
|
||||
private void TimeUpdate()
|
||||
{
|
||||
secondsOfDay++;
|
||||
}
|
||||
|
||||
|
||||
void timeUpdate()
|
||||
public float GetTimeOfDayFloat()
|
||||
{
|
||||
//Count
|
||||
minutes += 1;
|
||||
|
||||
countTime();
|
||||
countDate();
|
||||
|
||||
timeUI.text = current_time;
|
||||
|
||||
/*
|
||||
Debug.Log(current_time);
|
||||
Debug.Log(current_date);
|
||||
*/
|
||||
float timeOfDay = ((float)secondsOfDay / (float)3600);
|
||||
return timeOfDay;
|
||||
}
|
||||
|
||||
private void countTime()
|
||||
public string GetTimeString(bool seconds = false)
|
||||
{
|
||||
//Time
|
||||
if (minutes >= 60)
|
||||
{
|
||||
minutes = 0;
|
||||
hours += 1;
|
||||
}
|
||||
TimeSpan time = TimeSpan.FromSeconds(secondsOfDay);
|
||||
string str = "";
|
||||
|
||||
|
||||
//Current Time String
|
||||
if (hours <= 9)
|
||||
if (seconds)
|
||||
{
|
||||
current_time = "0" + hours + ":";
|
||||
str = time.ToString(@"hh\:mm\:ss");
|
||||
}
|
||||
else
|
||||
{
|
||||
current_time = hours + ":";
|
||||
str = time.ToString(@"hh\:mm");
|
||||
}
|
||||
|
||||
|
||||
if (minutes <= 9)
|
||||
{
|
||||
current_time += "0" + minutes;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_time += minutes;
|
||||
}
|
||||
}
|
||||
|
||||
private void countDate()
|
||||
{
|
||||
//Date
|
||||
if (hours >= 24)
|
||||
{
|
||||
hours = 0;
|
||||
day += 1;
|
||||
}
|
||||
else if (day > 31)
|
||||
{
|
||||
day = 1;
|
||||
month += 1;
|
||||
}
|
||||
else if (month > 12)
|
||||
{
|
||||
month = 1;
|
||||
year += 1;
|
||||
}
|
||||
|
||||
//Format Year
|
||||
if (year < 0)
|
||||
{
|
||||
yearStr = year * -1 + "B.C.";
|
||||
}
|
||||
else if (year > 0)
|
||||
{
|
||||
yearStr = year + "A.C.";
|
||||
}
|
||||
else
|
||||
{
|
||||
yearStr = "0";
|
||||
}
|
||||
|
||||
//current Date
|
||||
if (day <= 9)
|
||||
{
|
||||
current_date = "0" + day + "/";
|
||||
}
|
||||
else
|
||||
{
|
||||
current_date = day + "/";
|
||||
}
|
||||
|
||||
if (month <= 9)
|
||||
{
|
||||
current_date += "0" + month + "/";
|
||||
}
|
||||
else
|
||||
{
|
||||
current_date += month + "/";
|
||||
}
|
||||
|
||||
current_date += yearStr;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
22
Assets/Scripts/TimeTextScript.cs
Normal file
22
Assets/Scripts/TimeTextScript.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class TimeTextScript : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject GamerManager;
|
||||
|
||||
private string timeStr = "";
|
||||
private Text timeText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
timeText = GetComponent<Text>();
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
timeStr = GamerManager.GetComponent<TimeManager>().GetTimeString();
|
||||
timeText.text = timeStr;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/TimeTextScript.cs.meta
Normal file
11
Assets/Scripts/TimeTextScript.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 19f9379cc95b36d44bd902c97580c107
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user