Button appears when switching between scenes or quitting game
Scenario:
There are two Scenes - Scene A and B.
In Scene A there is a Watch Ad Reward Button with the below button cool down script.
Then user clicked Watch ad reward button. The button disappears. (I have set button to appear again after 1 hour).
When I go to Scene B and back to Scene A the button get appeared, also when quit the game. (So there is no point of 1 hour timer to appear the button).
What I need here is the button need to appear after 1 hour even when user switching between scene A and B or quit and restarting the game.
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ButtonCooldown : MonoBehaviour
{
[SerializeField]
Button myButton;
[SerializeField]
float cooldownDuration = 60f;
void Awake()
{
// Get a reference to your button
myButton = GetComponent<Button>();
if (myButton != null)
{
// Listen to its onClick event
myButton.onClick.AddListener(OnButtonClick);
}
}
// This method is called whenever myButton is pressed
void OnButtonClick()
{
StartCoroutine(Cooldown());
}
// Coroutine that will deactivate and reactivate the button
IEnumerator Cooldown()
{
// Deactivate myButton
myButton.interactable = false;
// Wait for cooldown duration
yield return new WaitForSeconds(cooldownDuration);
// Reactivate myButton
myButton.interactable = true;
}
}
Answer by AaronXRDev · Sep 28, 2020 at 04:11 PM
Here is a version of @dharmesh_b script that will use PlayerPrefs to save and reload the countdown timer.
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Button))]
public class ButtonCooldown : MonoBehaviour
{
[SerializeField]
[Tooltip("Make sure this value is unique for each cooldown button.")]
private string cooldownKeyName;
[SerializeField]
[Tooltip("Time in seconds that this button will be disabled after being pressed.")]
private float cooldownDuration = 60f;
[SerializeField]
[Tooltip("How often should this button to check to see if it should be enabled.")]
private float buttonCheckInterval = 1f;
Button myButton;
private float cooldownRemaining, lastKnownTime;
void Awake()
{
// Get a reference to your button
myButton = GetComponent<Button>();
myButton.onClick.AddListener(OnButtonClick);
if(cooldownKeyName != null && PlayerPrefs.HasKey(cooldownKeyName))
{
//If a cooldown key has been set, load it.
cooldownRemaining = PlayerPrefs.GetFloat(cooldownKeyName);
}
else
{
//If no key has been defined, set the cooldown to the max.
cooldownRemaining = cooldownDuration;
}
if (cooldownRemaining > 0)
{
//If there is still time left on the countdown, disable the button
myButton.interactable = false;
}
lastKnownTime = Time.time;
StartCoroutine(Cooldown());
}
private void OnButtonClick()
{
// Deactivate myButton
myButton.interactable = false;
//Reset this button's cooldown to the maximum and save to player prefs
cooldownRemaining = cooldownDuration;
//Set the last known time to the current time
lastKnownTime = Time.time;
PlayerPrefs.SetFloat(cooldownKeyName, cooldownRemaining);
}
// Coroutine that will reactivate the button
IEnumerator Cooldown()
{
while(true)
{
yield return new WaitForSeconds(buttonCheckInterval);
if (myButton.interactable == false)
{
//If the button iks disabled, countdown until reset
cooldownRemaining -= Time.time - lastKnownTime;
lastKnownTime = Time.time;
if (cooldownRemaining <= 0)
{
//If the countdown has passed, enable the button
cooldownRemaining = cooldownDuration;
myButton.interactable = true;
}
}
if (cooldownKeyName != null)
{
//Set the remaining time to this button's cooldown key to player prefs in case they stop the app
PlayerPrefs.SetFloat(cooldownKeyName, cooldownRemaining);
}
}
}
}
Your answer
Follow this Question
Related Questions
Script to random set active a child object? 0 Answers
how can i save highest played level?,how can i save number of highest played level? 0 Answers
How do i make an uppercut attack? 0 Answers
UnityCar - Controlling Speed with a duration? 2 Answers
Move to next animation with clicking the game object 0 Answers