Select Page

Unity3d daily reward internet time

Score 95%

Option 1: skip the work and get the asset: Unity Asset Store

Option 2:

follow the same instructions on : unity3d-internet-time-and-date-guide

<?php
 date_default_timezone_set('US/Eastern');
 $currenttime = date("m-d-Y H:i:s");
 list($ddd,$ttt) = split(' ',$currenttime);
 echo "$ddd/$ttt\n";
?

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

/// <summary>
/// Created by Leaton Mitchell 11/17/2017
/// Leatonm.net
/// </summary>
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeManager : MonoBehaviour {
    
    public static TimeManager sharedInstance = null;
    private string _url = "http://leatonm.net/wp-content/uploads/2017/candlepin/getdate.php"; //change this to your own
    private string _timeData;
    private string _currentTime;
    private string _currentDate;


    //make sure there is only one instance of this always.
    void Awake() {
        if (sharedInstance == null) {
            sharedInstance = this;
        } else if (sharedInstance != this) {
            Destroy (gameObject);  
        }
        DontDestroyOnLoad(gameObject);
    }


    //time fether coroutine
    public IEnumerator getTime()
    {
        Debug.Log ("connecting to php");
        WWW www = new WWW (_url);
        yield return www;
        if (www.error != null) {
            Debug.Log ("Error");
        } else {
            Debug.Log ("got the php information");
        }
        _timeData = www.text;
        string[] words = _timeData.Split('/');    
        //timerTestLabel.text = www.text;
        Debug.Log ("The date is : "+words[0]);
        Debug.Log ("The time is : "+words[1]);

        //setting current time
        _currentDate = words[0];
        _currentTime = words[1];
    }


    //get the current time at startup
    void Start()
    {
        Debug.Log ("TimeManager script is Ready.");
        StartCoroutine ("getTime");
    }

    //get the current date - also converting from string to int.
    //where 12-4-2017 is 1242017
    public int getCurrentDateNow()
    {
        string[] words = _currentDate.Split('-');
        int x = int.Parse(words[0]+ words[1] + words[2]);
        return x;
    }


    //get the current Time
    public string getCurrentTimeNow()
    {
        return _currentTime;
    }


}

sing System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DailyReward : MonoBehaviour {
    //UI
    public Text timeLabel; //only use if your timer uses a label
    public Button timerButton; //used to disable button when needed
    public Image _progress;
    //TIME ELEMENTS
    public int hours; //to set the hours
    public int minutes; //to set the minutes
    public int seconds; //to set the seconds
    private bool _timerComplete = false;
    private bool _timerIsReady;
    private TimeSpan _startTime;
    private TimeSpan _endTime;
    private TimeSpan _remainingTime;
    //progress filler
    private float _value = 1f;
    //reward to claim
    public int RewardToEarn;



    //startup
    void Start()
    {
        if (PlayerPrefs.GetString ("_timer") == "")
        { 
            Debug.Log ("==> Enableing button");
            enableButton ();
        } else 
        {
            disableButton ();
            StartCoroutine ("CheckTime");
        }
    }



    //update the time information with what we got some the internet
    private void updateTime()
    {
        if (PlayerPrefs.GetString ("_timer") == "Standby") {
            PlayerPrefs.SetString ("_timer", TimeManager.sharedInstance.getCurrentTimeNow ());
            PlayerPrefs.SetInt ("_date", TimeManager.sharedInstance.getCurrentDateNow());
        }else if (PlayerPrefs.GetString ("_timer") != "" && PlayerPrefs.GetString ("_timer") != "Standby")
        {
            int _old = PlayerPrefs.GetInt("_date");
            int _now = TimeManager.sharedInstance.getCurrentDateNow();
            
            
            //check if a day as passed
            if(_now > _old)
            {//day as passed
                Debug.Log("Day has passed");
                enableButton ();
                return;
            }else if (_now == _old)
            {//same day
                Debug.Log("Same Day - configuring now");
                _configTimerSettings();
                return;
            }else
            {
                Debug.Log("error with date");
                return;
            }
        }
         Debug.Log("Day had passed - configuring now");
         _configTimerSettings();
    }

//setting up and configureing the values
//update the time information with what we got some the internet
private void _configTimerSettings()
{
    _startTime = TimeSpan.Parse (PlayerPrefs.GetString ("_timer"));
    _endTime = TimeSpan.Parse (hours + ":" + minutes + ":" + seconds);
    TimeSpan temp = TimeSpan.Parse (TimeManager.sharedInstance.getCurrentTimeNow ());
    TimeSpan diff = temp.Subtract (_startTime);
    _remainingTime = _endTime.Subtract (diff);
    //start timmer where we left off
    setProgressWhereWeLeftOff ();
    
    if(diff >= _endTime)
    {
        _timerComplete = true;
        enableButton ();
    }else
    {
        _timerComplete = false;
        disableButton();
        _timerIsReady = true;
    }
}

//initializing the value of the timer
    private void setProgressWhereWeLeftOff()
    {
        float ah = 1f / (float)_endTime.TotalSeconds;
        float bh = 1f / (float)_remainingTime.TotalSeconds;
        _value = ah / bh;
        _progress.fillAmount = _value;
    }



    //enable button function
    private void enableButton()
    {
        timerButton.interactable = true;
        timeLabel.text = "CLAIM REWARD";
    }



    //disable button function
    private void disableButton()
    {
        timerButton.interactable = false;
        timeLabel.text = "NOT READY";
    }


    //use to check the current time before completely any task. use this to validate
    private IEnumerator CheckTime()
    {
        disableButton ();
        timeLabel.text = "Checking the time";
        Debug.Log ("==> Checking for new time");
        yield return StartCoroutine (
            TimeManager.sharedInstance.getTime()
        );
        updateTime ();
        Debug.Log ("==> Time check complete!");

    }


    //trggered on button click
    public void rewardClicked()
    {
        Debug.Log ("==> Claim Button Clicked");
        claimReward (RewardToEarn);
        PlayerPrefs.SetString ("_timer", "Standby");
        StartCoroutine ("CheckTime");
    }



    //update method to make the progress tick
    void Update()
    {
        if(_timerIsReady)
        {
            if (!_timerComplete && PlayerPrefs.GetString ("_timer") != "")
                {
                    _value -= Time.deltaTime * 1f / (float)_endTime.TotalSeconds;
                    _progress.fillAmount = _value;
                
                    //this is called once only
                    if (_value <= 0 && !_timerComplete) {
                        //when the timer hits 0, let do a quick validation to make sure no speed hacks.
                    validateTime ();
                    _timerComplete = true;
                }
            }
        }
    }



    //validator
    private void validateTime()
    {
        Debug.Log ("==> Validating time to make sure no speed hack!");
        StartCoroutine ("CheckTime");
    }


    private void claimReward(int x)
    {
        Debug.Log ("YOU EARN "+ x +" REWARDS");
    }

}

Review

95%

Summary Great information

95%

About The Author

Leaton Mitchell

Being smart doesn't count if you do not share your knowledge.