Overview

Using a TimeManager Script (You can use in multiple project without recoding anything)

Creating a daily and hourly reward system

A fully Independent Time System you can import into other Projects.

Why would you need a server time and date?

A secure 24 hour timer is very important for your Unity3d Project. It is a fact that players enjoy coming back to a game simply for the daily rewards, but without a proper time system in place, you game can be easily hacked. If your daily or hourly reward system is checking the platform time, then all the player has to do is change the time of their computer or phone and “Tada!”. With a internet base time and date, you can create a daily leaderboard by simply saving the players score and date, then return the scores matching the current or previous date.

Unity Internet Time and Date – setup php

Get time and date from php 2017

First thing we will need is a simple php file to fetch the server time from and drop it into unity. We use only one php file, which will return the time and date, then we will split the data and separate the time form the date in Unity. The php code we will use to get time and date from the internet is :

If you get an error with this php script. try replaceing “split” with the word “explode” this is the new way of splitting data in php5

you need a website or server to place this php some where out there on the web. I put mine in the back end of my WordPress site within my Cpanel. Keep a record of the direct url to that php file.

Unity Internet Time and Date – create time manager

Manage the time and date from the internet with unity:

Lets step over into unity with the path of our php file. We will create a new script and call it “TimeManager” This will be created using a Singleton pattern (look it up if you don’t know what it is). We want to make this TimeManager as independent as possible so that you can wrap it up after you’re done and reuse it within other projects. Here is the code we are going to use to manage the time and date from the internet with unity:

Price: $53.00

Was: $54.99

Price: $35.66

Was: $49.99

Unity avoid time cheat

Unity Internet time and date example script

The hard part is done. We now have everything setup how we want them. At this point we can make prefabs that will communicate this the “TimeManager” script from anywhere in the game.

Now that you have made a unhackable timer in unity, the rest is very simple. You can use “TimeManager.sharedInstance.getCurrentTimeNow()” to get the current time. I suggest calling the getTime function first and use coroutine to make sure it download the new time first before setting any text or variables. for example:

In this example above, I made a button with a text label to display the time. This game object will be your daily event user interface of your design. Simply slap this script on to it, assign the four public variables to the correct elements of your game object. Assign your button, timer label, then simply input the start and end time within the unity project inspector (use format 00″00″00).

Without the use of PlayerPrefs, this script will keep an accurate time of any daily events. It will let you know how long until the event starts, and how long before it ends.

Conclusion

Daily and hourly rewards have become essential to the mobile game industry, and should be used when every they can. The only issue is creating a timer that is not local to the device. The answer is to simply us the internet. For those of you asking “How to create a unity server base timer”, “How to create a daily reward system with Unity”or simply trying to know “How to build a unity countdown timer”, then I hope this answers your questions, or get you on the right track.