TimeOfDay

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Description)
m (Secondes -> seconds, pourentage -> percentage (in the code). Thank you CorrodedSoul, I mixed up french and english.)
Line 3: Line 3:
  
 
== Description ==
 
== Description ==
Compute the game time into hours, minutes and secondes depending on the duration of a day. Return either a string, a percentage or the components hours, minutes, seconds.
+
Compute the game time into hours, minutes and seconds depending on the duration of a day. Return either a string, a percentage or the components hours, minutes, seconds.
  
 
== Usage ==
 
== Usage ==
Line 16: Line 16:
 
void Update()
 
void Update()
 
{
 
{
print( TimeOfDay.GetTimeOfDay() + " Day : " + TimeOfDay.Day() );
+
print( TimeOfDay.GetTimeOfDay() + " Days : " + TimeOfDay.Days() );
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 29: Line 29:
 
public static class TimeOfDay  
 
public static class TimeOfDay  
 
{
 
{
// Day duration in secondes.
+
// Day duration in seconds.
 
public static float dayDuration{ get{ return m_fDayDuration; } set{ m_fDayDuration = value; m_fDayDurationInv = 1f / m_fDayDuration; } }
 
public static float dayDuration{ get{ return m_fDayDuration; } set{ m_fDayDuration = value; m_fDayDurationInv = 1f / m_fDayDuration; } }
 
private static float m_fDayDuration = 10f;
 
private static float m_fDayDuration = 10f;
Line 35: Line 35:
 
public static bool useRealTime = false;
 
public static bool useRealTime = false;
 
 
private static int m_iDay;
+
private static int m_iDays;
 
private static int m_iHours;
 
private static int m_iHours;
 
private static int m_iMinutes;
 
private static int m_iMinutes;
private static int m_iSecondes;
+
private static int m_iSeconds;
private static float m_fPourcentage;
+
private static float m_fPercentage;
 
 
 
private const int MINUTES_IN_ONE_HOUR = 60;
 
private const int MINUTES_IN_ONE_HOUR = 60;
private const int SECONDES_IN_ONE_HOUR = MINUTES_IN_ONE_HOUR*60;
+
private const int SECONDS_IN_ONE_HOUR = MINUTES_IN_ONE_HOUR*60;
private const int SECONDES_IN_ONE_DAY = SECONDES_IN_ONE_HOUR*24;
+
private const int SECONDS_IN_ONE_DAY = SECONDS_IN_ONE_HOUR*24;
 
 
 
public static void Sample()  
 
public static void Sample()  
 
{
 
{
 
float t = useRealTime ? Time.realtimeSinceStartup : Time.time;
 
float t = useRealTime ? Time.realtimeSinceStartup : Time.time;
m_fPourcentage = t % m_fDayDuration * m_fDayDurationInv;
+
m_fPercentage = t % m_fDayDuration * m_fDayDurationInv;
 
 
m_iSecondes = Mathf.RoundToInt( m_fPourcentage * SECONDES_IN_ONE_DAY );
+
m_iSeconds = Mathf.RoundToInt( m_fPercentage * SECONDS_IN_ONE_DAY );
m_iDay = Mathf.FloorToInt( t * m_fDayDurationInv );
+
m_iDays = Mathf.FloorToInt( t * m_fDayDurationInv );
m_iHours = m_iSecondes / SECONDES_IN_ONE_HOUR;
+
m_iHours = m_iSeconds / SECONDS_IN_ONE_HOUR;
m_iMinutes = (m_iSecondes % SECONDES_IN_ONE_HOUR) / MINUTES_IN_ONE_HOUR;
+
m_iMinutes = (m_iSeconds % SECONDS_IN_ONE_HOUR) / MINUTES_IN_ONE_HOUR;
m_iSecondes = (m_iSecondes % SECONDES_IN_ONE_HOUR) % MINUTES_IN_ONE_HOUR;
+
m_iSeconds = (m_iSeconds % SECONDS_IN_ONE_HOUR) % MINUTES_IN_ONE_HOUR;
 
}
 
}
 
 
Line 61: Line 61:
 
{
 
{
 
Sample();
 
Sample();
return string.Format ( format, m_iHours, m_iMinutes, m_iSecondes );
+
return string.Format ( format, m_iHours, m_iMinutes, m_iSeconds );
 
}
 
}
 
 
public static int Day(){ Sample(); return m_iDay; }
+
public static int Days(){ Sample(); return m_iDays; }
 
public static int Hours(){ Sample(); return m_iHours; }
 
public static int Hours(){ Sample(); return m_iHours; }
 
public static int Minutes(){ Sample(); return m_iMinutes; }
 
public static int Minutes(){ Sample(); return m_iMinutes; }
public static int Secondes(){ Sample(); return m_iSecondes; }
+
public static int Seconds(){ Sample(); return m_iSeconds; }
public static float Pourcentage(){ Sample(); return m_fPourcentage; }
+
public static float Percentage(){ Sample(); return m_fPercentage; }
 
// return 0 at midnight, 1 at noon and back at 0 at midnight
 
// return 0 at midnight, 1 at noon and back at 0 at midnight
public static float PourcentagePingPong(){ return Mathf.PingPong( Pourcentage() * 2, 1f ); }
+
public static float PercentagePingPong(){ return Mathf.PingPong( Percentage() * 2, 1f ); }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 00:09, 12 March 2012

Author: Berenger

Contents

Description

Compute the game time into hours, minutes and seconds depending on the duration of a day. Return either a string, a percentage or the components hours, minutes, seconds.

Usage

Just put the script in Standard Assets, and use it that way :

void Start()
{
	TimeOfDay.dayDuration = 10f;
}
 
void Update()
{
	print( TimeOfDay.GetTimeOfDay() + " Days : " + TimeOfDay.Days() );
}


Code - CSharp

TimeOfDay.cs

using UnityEngine;
using System.Collections;
 
public static class TimeOfDay 
{	
	// Day duration in seconds.
	public static float dayDuration{ get{ return m_fDayDuration; } set{ m_fDayDuration = value; m_fDayDurationInv = 1f / m_fDayDuration; } }
	private static float m_fDayDuration = 10f;
	private static float m_fDayDurationInv = 0.1F;
	public static bool useRealTime = false;
 
	private static int m_iDays;
	private static int m_iHours;
	private static int m_iMinutes;
	private static int m_iSeconds;
	private static float m_fPercentage;
 
	private const int MINUTES_IN_ONE_HOUR = 60;
	private const int SECONDS_IN_ONE_HOUR = MINUTES_IN_ONE_HOUR*60;
	private const int SECONDS_IN_ONE_DAY = SECONDS_IN_ONE_HOUR*24;
 
	public static void Sample() 
	{
		float t = useRealTime ? Time.realtimeSinceStartup : Time.time;
		m_fPercentage = t % m_fDayDuration * m_fDayDurationInv;
 
		m_iSeconds = Mathf.RoundToInt( m_fPercentage * SECONDS_IN_ONE_DAY );
		m_iDays = Mathf.FloorToInt( t * m_fDayDurationInv );
		m_iHours = m_iSeconds / SECONDS_IN_ONE_HOUR;
		m_iMinutes = (m_iSeconds % SECONDS_IN_ONE_HOUR) / MINUTES_IN_ONE_HOUR;
		m_iSeconds = (m_iSeconds % SECONDS_IN_ONE_HOUR) % MINUTES_IN_ONE_HOUR;
	}
 
	public static string GetTimeOfDay(){ return GetTimeOfDay("{0:00}:{1:00}:{2:00}"); }
	public static string GetTimeOfDay( string format )
	{
		Sample();
		return string.Format ( format, m_iHours, m_iMinutes, m_iSeconds );
	}
 
	public static int Days(){ Sample(); return m_iDays; }
	public static int Hours(){ Sample(); return m_iHours; }
	public static int Minutes(){ Sample(); return m_iMinutes; }
	public static int Seconds(){ Sample(); return m_iSeconds; }
	public static float Percentage(){ Sample(); return m_fPercentage; }
	// return 0 at midnight, 1 at noon and back at 0 at midnight
	public static float PercentagePingPong(){ return Mathf.PingPong( Percentage() * 2, 1f ); }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox