Thursday, January 14, 2021

Add Timer In games or make Reward system in game

Let's Add a Reward system in-game, Set or calculate seconds minutes, and hours in the game.

Here is an example showing how to add a timer in-game

1) Create a unity3d project.

2) Create a new script like TimeManager.

3) Put in Scene empty object or where you need.

4) Add below Code.

using System;

using System.Collections;

using System.Collections.Generic;

using System.Globalization;

using UnityEngine;

using UnityEngine.UI;

public class TimeManager : MonoBehaviour

{

    public static TimeManager instance;

    public Text TimeTexttoDisplay;

    public Button Btn_intractable;

    public DateTime lastRewardTime, now;

    public TimeSpan timer;

    public float maxTime = 3600f;

    public delegate void OnClaimReward(int index);

    public static OnClaimReward onClaimReward;

    private bool canClaim;

    public delegate void OnCanClaim();

    public static OnCanClaim onCanClaim;

    private const string TIMED_REWARDS_TIME = "TimedRewardsTime";

    private const string FMT = "O";

    public bool isInitialized = false;

    private void Awake()

    {

       instance = this;

    }

    void Start()

    {

        StartCoroutine(InitializeTimer());

    }

    private IEnumerator InitializeTimer()  // set timer using playerprefs

    {

        now = DateTime.Now;

        isInitialized = true;

        string lastRewardTimeStr = PlayerPrefs.GetString(TIMED_REWARDS_TIME);

        if (!string.IsNullOrEmpty(lastRewardTimeStr))

        {

            lastRewardTime = DateTime.ParseExact(lastRewardTimeStr, FMT, CultureInfo.InvariantCulture);

            timer = (lastRewardTime - now).Add(TimeSpan.FromSeconds(maxTime));

            Debug.Log(timer);

        }

        else     {

            timer = TimeSpan.FromSeconds(maxTime);

            Debug.Log(timer);

        }

        yield return new WaitForSeconds(0.1f);

    }

    void Update()

    {

        if (!isInitialized)

        {

            return;

        }

        now = now.AddSeconds(Time.unscaledDeltaTime);

        if (!canClaim)

        {

            timer = timer.Subtract(TimeSpan.FromSeconds(Time.unscaledDeltaTime));

            if (timer.TotalSeconds <= 0)

            {

                canClaim = true;

                Btn_intractable.interactable=true;// if time <= 0 we need to spinbtn interactable true ,  time                     finish

                TimeTexttoDisplay.text = "spin is open ";

                Debug.Log("now add new spin");

                ClaimReward(0); // this is click event for what we do after remaing time = 0,

                if (onCanClaim != null)

                {

                    onCanClaim();

                }

            }

            else

            {

                //Time Calculation 

                PlayerPrefs.SetString(TIMED_REWARDS_TIME, now.Add(timer -                                                          TimeSpan.FromSeconds(maxTime)).ToString(FMT));

                TimeSpan timers = timer;

                if (timers.TotalSeconds > 0)// if time >0 we need to spinbtn interactable false

                {

                    Btn_intractable.interactable = false;

                    TimeTexttoDisplay.text = "Spin in " + string.Format("{0:D2}:{1:D2}:{2:D2}",                                       timer.Hours, timer.Minutes, timer.Seconds);

                }

            }

        }

    }

    public void ClaimReward(int rewardIdx)    

    {

        if (tempspin <= 10)

        {

            PlayerPrefs.SetString(TIMED_REWARDS_TIME, now.Add(timer - TimeSpan.FromSeconds(maxTime)).ToString(FMT));

            timer = TimeSpan.FromSeconds(maxTime);

            canClaim = false;

            if (onClaimReward != null)

            {

                onClaimReward(rewardIdx);

            }         

        }

    }

    public void Reset()

    {

        PlayerPrefs.DeleteKey(TIMED_REWARDS_TIME);

        canClaim = true;

        timer = TimeSpan.FromSeconds(0);


        if (onCanClaim != null)

        {

            onCanClaim();

        }

    }

}

5) Give Reference of button and Text to display timer.

6)In script maxTime variable is to set your timer.

No comments:

Post a Comment

Privacy Policy

  HEPEE GAMES Privacy Policy Hepee Games issues this Privacy Policy to inform you of our procedures and policies regarding the collection, d...