Simple Day/Night Cycle
Posted: Sun Apr 19, 2020 12:31 pm
Good to see you Thanks you looking at this Script
This Script is made for Beginners who want use a Day/Night Cycle in Unity its not perfect or something its just a good Template for extending it^^
if you guys have a better way write a comment!
TimeManager_Local.cs
You can use the code without any credits for me!
Have Fun and Work hard!
This Script is made for Beginners who want use a Day/Night Cycle in Unity its not perfect or something its just a good Template for extending it^^
if you guys have a better way write a comment!
TimeManager_Local.cs
Code: Select all
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* File: TimeManager_Local
* Desc: This is a Simple Script for TimeChange In-Game
*
* Features: GameTime or RealTime
*
*/
public class TimeManager_Local : MonoBehaviour
{
public enum TimeType { GameTime,RealTime }
[Header("Time Manager Settings")]
[Tooltip("Change time mode between GameTime or RealTime!")]
public TimeType TimeMode;
int hour, minutes, seconds;
private void Start()
{
//
// First we check what type we using!
//
if(TimeMode == TimeType.GameTime)
{
GetGameTime();
}
else
{
GetRealTime();
}
}
void Update()
{
// Apply Rotation with Math^^
float curTime = 0f;
curTime += hour * 3600f; curTime += minutes * 60f; curTime += seconds;
float rot = 360 * (curTime / 86400f) -90;
transform.rotation=Quaternion.Euler(rot, -20, 0);
curTime = 0f;
}
private void GetRealTime()
{
//In RealTime Mode we use the System time of your PC
hour = DateTime.Now.Hour;
minutes = DateTime.Now.Minute;
seconds = DateTime.Now.Second;
StartCoroutine(RealTimeNext());
}
IEnumerator RealTimeNext()
{
// Refresh every second!
yield return new WaitForSeconds(1f);
GetRealTime();
}
private void GetGameTime()
{
// using GameTime can be changed here
hour = 12;
minutes = 0;
seconds = 0;
StartCoroutine(GameTimeNext());
}
IEnumerator GameTimeNext()
{
seconds++; // Add 1 Second
yield return new WaitForSeconds(1f);
if(seconds == 60) // do we have 60 Seconds?
{
seconds = 0; // set it to Zero!
minutes++; // Add 1 Minute
if(minutes == 60) // Wait do we have 60 minutes reached?
{
minutes = 0; // set it to zero
hour++; // adding a Hour
if(hour == 24) // Wait do we have 24 hours already?
{
hour = 0;// then zero it^^
}
}
}
StartCoroutine(GameTimeNext());
}
}
Have Fun and Work hard!