There was a problem with saving the date. I generate a date by adding two minutes to the current date and time. after restarting the program, the date is not saved.

enter image description here

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class NewBehaviourScript : MonoBehaviour { public Text timenow; public Text time; public DateTime timerSob; private Save da = new Save(); private void Awake() { if (PlayerPrefs.HasKey("da")) { da = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("da")); timerSob = da.timerSob; } } void Update() { timenow.text = Convert.ToString(DateTime.Now); time.text = Convert.ToString(timerSob); } public void timer() { timerSob = DateTime.Now.AddMinutes(2); } #if UNITY_ANDROID && !UNITY_EDITOR //ПРОЦЕСС СОХРАНЕНИЯ ДАННЫХ ИГРЫ private void OnApplicationPause(bool pause) { if (pause) { PlayerPrefs.SetString("da", JsonUtility.ToJson(da)); da.timerSob = timerSob; } } #else private void OnApplicationQuit() { da.timerSob = timerSob; PlayerPrefs.SetString("da", JsonUtility.ToJson(da)); } #endif [System.Serializable] public class Save { public System.DateTime timerSob; } } 
  • Need to save a date? - Andrew

1 answer 1

To download, use:

 time = DateTime.Parse(PlayerPrefs.GetString("Time")) 

To save:

 PlayerPrefs.SetString("Time", time.ToString()); 

And in the code, work with the time variable, whose type is DateTime .

  • thanks, it helped - Vladislav Sit