Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Unity problem when i convert playerprefs save to the standard file io save

$
0
0
i am using tow kind of save the main save which is encrypting save that is using in this video (https://www.youtube.com/watch?v=XOjd_qU2Ido&t=351s) and playerprefs save when i use them together with the playerprefs only works when i disable the playerprefs the encrypting save work how to make them work together the playerprefs comes as cs script when i bought daily reward from the asset store this the cs script that contain the playerprefs so i got a problem when i convert playerprefs save to the standard file io save any help using UnityEngine; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; namespace NiobiumStudios { /** * Timed Rewards Canvas is the User interface to show Timed rewards **/ public class TimedRewards : DailyRewardsCore { public DateTime lastRewardTime; // The last time the user clicked in a reward public TimeSpan timer; public float maxTime = 3600f; // How many seconds until the player can claim the reward public bool isRewardRandom = false; // Is the reward random or the player chooses from available rewards? public List rewards; public delegate void OnCanClaim(); // When the player can claim the reward public OnCanClaim onCanClaim; public delegate void OnClaimPrize(int index); // When the player claims the prize public OnClaimPrize onClaimPrize; private bool canClaim; // Checks if the user can claim the reward // Needed Constants private const string TIMED_REWARDS_TIME = "TimedRewardsTime"; private const string FMT = "O"; void Start() { StartCoroutine(InitializeTimer()); } // Initializes the timer in case the user already have a player preference private IEnumerator InitializeTimer() { yield return StartCoroutine(base.InitializeDate()); if (base.isErrorConnect) { if(onInitialize!=null) onInitialize(true, base.errorMessage); } else { LoadTimerData(); if(onInitialize!=null) onInitialize(); CheckCanClaim(); } } void LoadTimerData () { string lastRewardTimeStr = PlayerPrefs.GetString(GetTimedRewardsTimeKey()); if (!string.IsNullOrEmpty(lastRewardTimeStr)) { lastRewardTime = DateTime.ParseExact(lastRewardTimeStr, FMT, CultureInfo.InvariantCulture); timer = (lastRewardTime - now).Add(TimeSpan.FromSeconds(maxTime)); } else { timer = TimeSpan.FromSeconds(maxTime); } } protected override void OnApplicationPause(bool pauseStatus) { base.OnApplicationPause(pauseStatus); LoadTimerData(); CheckCanClaim(); } public override void TickTime() { base.TickTime(); if(isInitialized) { // Keeps ticking until the player claims if (!canClaim) { timer = timer.Subtract(TimeSpan.FromSeconds(Time.unscaledDeltaTime)); CheckCanClaim(); } } } public void CheckCanClaim () { if (timer.TotalSeconds <= 0) { canClaim = true; if (onCanClaim != null) onCanClaim(); } else { // We need to save the player time every tick. If the player exits the game the information keeps logged // For perfomance issues you can save this information when the player switches scenes or quits the application PlayerPrefs.SetString(GetTimedRewardsTimeKey(), now.Add(timer - TimeSpan.FromSeconds(maxTime)).ToString(FMT)); } } //Returns the TimeRewardsTime playerPrefs key depending on instanceId private string GetTimedRewardsTimeKey() { if (instanceId == 0) return TIMED_REWARDS_TIME; return string.Format("{0}_{1}", TIMED_REWARDS_TIME, instanceId); } // The player claimed the prize. We need to reset to restart the timer public void ClaimReward(int rewardIdx) { PlayerPrefs.SetString(GetTimedRewardsTimeKey(), now.Add(timer - TimeSpan.FromSeconds(maxTime)).ToString(FMT)); timer = TimeSpan.FromSeconds(maxTime); canClaim = false; if (onClaimPrize != null) onClaimPrize(rewardIdx); } public string GetFormattedTime() { if(timer.Days > 0) return string.Format("{0:D2} days {1:D2}:{2:D2}:{0:D3}", timer.Days, timer.Hours, timer.Minutes, timer.Seconds); else return string.Format("{0:D2}:{1:D2}:{2:D2}", timer.Hours, timer.Minutes, timer.Seconds); } // Resets the Timed Rewards. For debug purposes public void Reset() { PlayerPrefs.DeleteKey(GetTimedRewardsTimeKey()); canClaim = true; timer = TimeSpan.FromSeconds(0); if (onCanClaim != null) onCanClaim(); } // Returns a reward from an index public Reward GetReward(int index) { return rewards[index]; } } }

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>