// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; using Doozy.Runtime.Common; using Doozy.Runtime.Common.Utils; using Doozy.Runtime.UIManager.Content.Internal; using UnityEngine; // ReSharper disable ClassWithVirtualMembersNeverInherited.Global namespace Doozy.Runtime.UIManager.Content { /// /// The UIClock component is used to display the current time in one or more text components. /// It works with both real time (unscaled time) and game time (scaled time). /// [AddComponentMenu("Doozy/UI/DateTime/UI Clock")] public class UIClock : DateTimeComponent { #if UNITY_EDITOR [UnityEditor.MenuItem("GameObject/Doozy/UI/DateTime/UI Clock", false, 8)] private static void CreateComponent(UnityEditor.MenuCommand menuCommand) { GameObjectUtils.AddToScene("UIClock", false, true); } #endif [SerializeField] private string TimeZoneId; /// Time zone ID for the clock public string timeZoneId { get => TimeZoneId; set { if (timeZoneInfo.Id.Equals(value)) return; timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(value); TimeZoneId = value; TimeZoneChanged(); } } private TimeZoneInfo m_TimeZoneInfo = TimeZoneInfo.Local; /// The time zone info of the clock public TimeZoneInfo timeZoneInfo { get => m_TimeZoneInfo; set { m_TimeZoneInfo = value; TimeZoneId = value.Id; TimeZoneChanged(); } } #if UNITY_EDITOR protected override void Reset() { base.Reset(); if(labels.Count == 0) labels.Add(new FormattedLabel(null)); OnStartBehaviour = OnStartBehaviour = TimerBehaviour.Disabled; OnEnableBehaviour = OnEnableBehaviour = TimerBehaviour.ResetAndStart; OnDisableBehaviour = OnDisableBehaviour = TimerBehaviour.Stop; OnDisableBehaviour = OnDisableBehaviour = TimerBehaviour.Cancel; SetUtcTimeZone(); } #endif // UNITY_EDITOR protected override void OnEnable() { base.OnEnable(); StartTimer(); } protected override void OnDisable() { base.OnDisable(); StopTimer(); } public override void StartTimer() { base.StartTimer(); UpdateLabels(); } public override void StopTimer() { base.StopTimer(); UpdateLabels(); } public override void ResetTimer() { base.ResetTimer(); UpdateLabels(); } public override void PauseTimer() { base.PauseTimer(); UpdateLabels(); } public override void ResumeTimer() { base.ResumeTimer(); UpdateLabels(); } public override void CancelTimer() { base.CancelTimer(); UpdateLabels(); } protected override void SetStartTime() { //set start time according to the current time zone startTime = TimeZoneInfo.ConvertTimeFromUtc(GetDateTimeUtcNow(), timeZoneInfo); UpdateLastTime(); } protected override void SetEndTime() { endTime = startTime .AddYears(100); } public void TimeZoneChanged() { SetStartTime(); SetEndTime(); UpdateCurrentTime(); } protected override void UpdateCurrentTime() { currentTime = TimeZoneInfo.ConvertTimeFromUtc(GetDateTimeUtcNow(), timeZoneInfo); Years = currentTime.Year; Months = currentTime.Month; Days = currentTime.Day; Hours = currentTime.Hour; Minutes = currentTime.Minute; Seconds = currentTime.Second; Milliseconds = currentTime.Millisecond; UpdateLabels(); } public virtual DateTime GetDateTimeUtcNow() { return DateTime.UtcNow; } public void SetTimeZone(string zoneId) { timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(zoneId); TimeZoneId = timeZoneInfo.Id; TimeZoneChanged(); } public int GetTimeZoneUtcOffset() => timeZoneInfo.BaseUtcOffset.Hours; public void SetTimeZone(TimeZoneInfo zoneInfo) { timeZoneInfo = zoneInfo; TimeZoneId = timeZoneInfo.Id; TimeZoneChanged(); } /// /// Set the time zone for the clock by using the UTC offset /// /// public void SetTimeZoneByUtcOffset(int utcOffset) { string zoneId = "UTC" + (utcOffset >= 0 ? "+" : "") + utcOffset; timeZoneInfo = TimeZoneInfo.CreateCustomTimeZone(zoneId, TimeSpan.FromHours(utcOffset), zoneId, zoneId); TimeZoneId = timeZoneInfo.Id; TimeZoneChanged(); } /// /// Sets the local timezone as the clock's time zone /// public void SetLocalTimeZone() => SetTimeZone(TimeZoneInfo.Local); /// /// Sets the UTC as the clock's time zone /// public void SetUtcTimeZone() => SetTimeZone(TimeZoneInfo.Utc); } }