Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- //Script by Andriy Skyba
- namespace FeelkepStudio
- {
- public class SyncTimeSystem : MonoBehaviour
- {
- // Text to show count of coins
- [SerializeField] Text earnedcoinsText;
- // Timer
- float timer = 10;
- // Count of coins
- int earnedcoins;
- void Start()
- {
- // If key not found
- if (!PlayerPrefs.HasKey("Coins"))
- // Then set value of coins 0 (default)
- PlayerPrefs.SetInt("Coins", 0);
- // Get coins of PlayerPrefs key
- earnedcoins = PlayerPrefs.GetInt("Coins");
- int сurrentTimeToExit = PlayerPrefs.GetInt("LastSession");
- // Get now time in seconds
- int nowAllSeconds = GetNowTimeInSeconds();
- // Get absence time
- int absenceTime = nowAllSeconds - сurrentTimeToExit;
- // How long was the player absent
- Debug.Log("You were absent: " + absenceTime + " seconds");
- // Parsing leave time to coins (10 seconds = 1$ (1 coin))
- earnedcoins += absenceTime / 10;
- Debug.Log("You earned: " + absenceTime / 10 + " coins!");
- // Parsing to date time
- int hours = 0, minutes = 0, seconds = 0;
- // Parsing leave seconds to hour
- if (absenceTime / 3600 > 0)
- {
- // Divide seconds by one hour in seconds, namely (3600)
- hours = absenceTime / 3600;
- // Get seconds from the resulting hour
- absenceTime -= hours * 3600;
- }
- // Parsing leave seconds to minutes
- if (absenceTime / 60 > 0)
- {
- // Divide seconds by one minute in seconds, namely (60)
- minutes = absenceTime / 60;
- // Get seconds from the resulting minutes
- absenceTime -= minutes * 60;
- }
- // Just equals last time to seconds
- seconds = absenceTime;
- // Debug
- Debug.Log("You were absent:\nHours: " + hours + " | Minutes: " + minutes + " | Seconds: " + seconds);
- // Equate count of coins to text
- earnedcoinsText.text = earnedcoins.ToString() + "$";
- }
- void Update()
- {
- // Simple cooldown timer
- if (timer > 0)
- timer -= Time.deltaTime;
- // If timer < 0
- else if (timer <= 0)
- {
- // Set timer to 10
- timer = 10;
- // Add 1 coin
- earnedcoins += 1;
- // Equate count of coins to text
- earnedcoinsText.text = earnedcoins.ToString() + "$";
- }
- }
- // Reset value of coins
- public void ResetCoinsValue()
- {
- // Set value of coins 0
- earnedcoins = 0;
- // Save value of coins
- PlayerPrefs.SetInt("Coins", earnedcoins);
- }
- // Getting now time in seconds
- int GetNowTimeInSeconds()
- {
- // New variable
- int allSeconds = 0;
- // Parsing hour to allSeconds
- allSeconds += DateTime.Now.Hour * 3600;
- // Parsing minute to allSeconds
- allSeconds += DateTime.Now.Minute * 60;
- // Add now seconds to allSeconds
- allSeconds += DateTime.Now.Second;
- // Returning result
- return allSeconds;
- }
- // If player is quit
- private void OnApplicationQuit()
- {
- // Debug
- Debug.Log("Saving last session...");
- // Getting now time in seconds
- int allseconds = GetNowTimeInSeconds();
- // Save current value of coins
- PlayerPrefs.SetInt("Coins", earnedcoins);
- // Save now time
- PlayerPrefs.SetInt("LastSession", allseconds);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment