Resolve:
TimeManager:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TimeManager : MonoBehaviour {
static TimeManager instance;
public string timeGameObjectName;
int day;
int hour;
int minute;
float second = -1;
int worldTimeSpeed = 1;
string currentTime = "";
float delay = 3;
float updateTime = -1;
float lastFrameTime = -1;
// Use this for initialization
void Start () {
instance = this;
NetworkAPI.RegisterExtensionMessageHandler("server_time", ServerTimeMessage);
}
// Update is called once per frame
void Update () {
if (second == -1)
return;
// Recalculate the current time
second += (Time.realtimeSinceStartup - lastFrameTime) * worldTimeSpeed;
lastFrameTime = Time.realtimeSinceStartup;
if (second >= 60) {
int minutesPassed = (int)second / 60;
second -= 60 * minutesPassed;
minute += minutesPassed;
if (minute >= 60) {
minute -= 60;
hour += 1;
}
if (hour >= 24) {
hour -= 24;
day += 1;
}
// Send out a time update message since the minute/hour has changed
string[] args = new string[1];
AtavismEventSystem.DispatchEvent("WORLD_TIME_UPDATE", args);
//Debug.Log("Time is now: " + hour + ":" + minute + " with deltaTime: " + Time.deltaTime + " and minutesPassed: " + minutesPassed);
}
if (timeGameObjectName != "" && updateTime != -1 && Time.time > updateTime) {
GameObject timeReqGameObject = GameObject.Find(timeGameObjectName);
if (timeReqGameObject != null) {
timeReqGameObject.SendMessage("SetSecond", second);
timeReqGameObject.SendMessage("SetMinute", minute);
timeReqGameObject.SendMessage("SetHour", hour);
timeReqGameObject.SendMessage("SetDay", day);
}
updateTime = -1;
}
}
public void ServerTimeMessage(Dictionary props) {
day = (int)props["day"];
hour = (int)props["hour"];
minute = (int)props["minute"];
second = (int)props["second"];
worldTimeSpeed = (int)props["worldTimeSpeed"];
//Debug.Log("Got Server Time Message with Day: " + day + ", hour: " + hour + ". minute: " + minute);
lastFrameTime = Time.realtimeSinceStartup;
updateTime = Time.time + delay;
}
public static TimeManager Instance {
get {
return instance;
}
}
public string CurrentTime {
get {
return currentTime;
}
}
public int Hour {
get {
return hour;
}
}
public int Minute {
get {
return minute;
}
}
public float Second
{
get {
return second;
}
}
public int Day
{
get
{
return day;
}
}
}
UGUIWorldTimeDisplayPersonal:
using UnityEngine;
using System;
using UnityEngine.UI;
using System.Collections;
public class UGUIWorldTimeDisplayPersonal : MonoBehaviour {
//
public GameObject uniStormSystem;
//
public string day;
//
public int Month;
//
public int Year;
//
public string hour;
//
public string minute;
//
public string second;
//
public string timeText;
//
public string dateText;
//
void Awake()
{
//
uniStormSystem = GameObject.Find("UniStormSystemEditor");
//
}
//
// Use this for initialization
//
void Start () {
//
AtavismEventSystem.RegisterEvent("WORLD_TIME_UPDATE", this);
//
Month = System.DateTime.Now.Month;
//
Year = System.DateTime.Now.Year;
//
uniStormSystem.GetComponent().monthCounter = Month;
//
uniStormSystem.GetComponent().yearCounter = Year;
//
UpdateTimeDisplay();
//
}
//
// Update is called once per frame
//
void Update()
{
//
dateText = System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");
//
UpdateTimeDisplay();
//
}
//
void OnDestroy() {
//
AtavismEventSystem.UnregisterEvent("WORLD_TIME_UPDATE", this);
//
}
//
public void OnEvent(AtavismEventData eData) {
//
if (eData.eventType == "WORLD_TIME_UPDATE") {
//
UpdateTimeDisplay();
//
}
//
}
//
void UpdateTimeDisplay() {
//
if (timeText != null) {
//
// Get time from the time system and set it to the text
//
hour = TimeManager.Instance.Hour.ToString();
//
if (TimeManager.Instance.Hour < 10) {
//
hour = "0" + hour;
//
}
//
minute = TimeManager.Instance.Minute.ToString();
//
if (TimeManager.Instance.Minute < 10) {
//
minute = "0" + minute;
//
}
//
second = TimeManager.Instance.Second.ToString();
//
if (TimeManager.Instance.Second < 10) {
//
second = "0" + second;
//
}
//
day = TimeManager.Instance.Day.ToString();
//
timeText = day + " / " + hour + ":" + minute + ":" + second;
//
//Hour = uniStormSystem.GetComponent().hourCounter;
//
uniStormSystem.GetComponent().dayCounter = TimeManager.Instance.Day;
//
uniStormSystem.GetComponent().hourCounter = TimeManager.Instance.Hour;
//
uniStormSystem.GetComponent().minuteCounter = TimeManager.Instance.Minute;
//
}
//
}
//
}