├── Assets ├── Test.cs ├── Test.cs.meta ├── WorldTimeAPI.cs ├── WorldTimeAPI.cs.meta ├── scene.unity └── scene.unity.meta └── README.md /Assets/Test.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.UI; 3 | using System; 4 | 5 | public class Test : MonoBehaviour { 6 | [SerializeField] Text datetimeText; 7 | 8 | void Update ( ) { 9 | if ( Input.GetMouseButtonUp ( 0 ) && WorldTimeAPI.Instance.IsTimeLodaed ) { 10 | DateTime currentDateTime = WorldTimeAPI.Instance.GetCurrentDateTime ( ); 11 | 12 | datetimeText.text = currentDateTime.ToString ( ); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Assets/Test.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 75c369222e2c91f4d9c8067cc4f4b473 3 | timeCreated: 1597419826 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/WorldTimeAPI.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Collections; 4 | using System.Text.RegularExpressions; 5 | using UnityEngine.Networking; 6 | 7 | public class WorldTimeAPI : MonoBehaviour { 8 | #region Singleton class: WorldTimeAPI 9 | 10 | public static WorldTimeAPI Instance; 11 | 12 | void Awake ( ) { 13 | if ( Instance == null ) { 14 | Instance = this; 15 | DontDestroyOnLoad ( this.gameObject ); 16 | } else { 17 | Destroy ( this.gameObject ); 18 | } 19 | } 20 | 21 | #endregion 22 | 23 | //json container 24 | struct TimeData { 25 | //public string client_ip; 26 | //... 27 | public string datetime; 28 | //.. 29 | } 30 | 31 | const string API_URL = "http://worldtimeapi.org/api/ip"; 32 | 33 | [HideInInspector] public bool IsTimeLodaed = false; 34 | 35 | private DateTime _currentDateTime = DateTime.Now; 36 | 37 | void Start ( ) { 38 | StartCoroutine ( GetRealDateTimeFromAPI ( ) ); 39 | } 40 | 41 | public DateTime GetCurrentDateTime ( ) { 42 | //here we don't need to get the datetime from the server again 43 | // just add elapsed time since the game start to _currentDateTime 44 | 45 | return _currentDateTime.AddSeconds ( Time.realtimeSinceStartup ); 46 | } 47 | 48 | IEnumerator GetRealDateTimeFromAPI ( ) { 49 | UnityWebRequest webRequest = UnityWebRequest.Get ( API_URL ); 50 | Debug.Log ( "getting real datetime..." ); 51 | 52 | yield return webRequest.SendWebRequest ( ); 53 | 54 | if ( webRequest.isNetworkError || webRequest.isHttpError ) { 55 | //error 56 | Debug.Log ( "Error: " + webRequest.error ); 57 | 58 | } else { 59 | //success 60 | TimeData timeData = JsonUtility.FromJson ( webRequest.downloadHandler.text ); 61 | //timeData.datetime value is : 2020-08-14T15:54:04+01:00 62 | 63 | _currentDateTime = ParseDateTime ( timeData.datetime ); 64 | IsTimeLodaed = true; 65 | 66 | Debug.Log ( "Success." ); 67 | } 68 | } 69 | //datetime format => 2020-08-14T15:54:04+01:00 70 | DateTime ParseDateTime ( string datetime ) { 71 | //match 0000-00-00 72 | string date = Regex.Match ( datetime, @"^\d{4}-\d{2}-\d{2}" ).Value; 73 | 74 | //match 00:00:00 75 | string time = Regex.Match ( datetime, @"\d{2}:\d{2}:\d{2}" ).Value; 76 | 77 | return DateTime.Parse ( string.Format ( "{0} {1}", date, time ) ); 78 | } 79 | } 80 | 81 | 82 | /* API (json) 83 | { 84 | "abbreviation" : "+01", 85 | "client_ip" : "190.107.125.48", 86 | "datetime" : "2020-08-14T15:544:04+01:00", 87 | "dst" : false, 88 | "dst_from" : null, 89 | "dst_offset" : 0, 90 | "dst_until" : null, 91 | "raw_offset" : 3600, 92 | "timezone" : "Asia/Brunei", 93 | "unixtime" : 1595601262, 94 | "utc_datetime" : "2020-08-14T15:54:04+00:00", 95 | "utc_offset" : "+01:00" 96 | } 97 | 98 | We only need "datetime" property. 99 | */ 100 | -------------------------------------------------------------------------------- /Assets/WorldTimeAPI.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: b940a82b09855464bbec8f94089f3f6e 3 | timeCreated: 1597416358 4 | licenseType: Pro 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/scene.unity: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/herbou/Unity_GetRealDateTime/69a51bf687236ae3db0a01665f79d7587b1ef317/Assets/scene.unity -------------------------------------------------------------------------------- /Assets/scene.unity.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 162692f928d05ab4e9f78ca5eef58510 3 | timeCreated: 1596456884 4 | licenseType: Pro 5 | DefaultImporter: 6 | userData: 7 | assetBundleName: 8 | assetBundleVariant: 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Get real date & time from an API 2 | ## Prevent cheating by changing device's time or date. 3 | 4 | [↪Watch Video Tutorial ](https://www.youtube.com/watch?v=uJK1ajLaq6I) 5 | 6 | ### If you want to use your own API using php : 7 | - add a folder to your server named `TimeApi` 8 | - create an `index.php` file inside of TimeApi/ 9 | ```php 10 | 16 | ``` 17 | 18 | Now you can get time from your server: 19 | `http://yourdomainname.com/TimeApi/` 20 | 21 | ### If you don't want to use the json format : 22 | ```php 23 | 28 | ``` 29 | But make sure to remove the `JsonUtility..` code from `WorldTimeAPI.cs` in the `Assets` folder and use this one : 30 | ```c# 31 | IEnumerator GetRealDateTimeFromAPI ( ) { 32 | //... 33 | //... 34 | } else { 35 | //success 36 | TimeData timeData = new TimeData( ); 37 | timeData.datetime = webRequest.downloadHandler.text; 38 | //.... 39 | } 40 | } 41 | ``` 42 | 43 | 44 |

45 |
46 | ## ❤️ Donate 47 | Paypal 48 | --------------------------------------------------------------------------------