S
S
Stanislav Ivanov2021-03-10 22:22:40
C++ / C#
Stanislav Ivanov, 2021-03-10 22:22:40

How to save date in Unity to Json file?

Good afternoon, at the moment I'm trying to save the date to the file so that when I enter the game, a message appears about the date of the last entry into the game, however, when the date is pulled out of the file, the initial date is obtained (01.01.0001 0:00:00), do not tell me what problem? If anything, the DataSave method saves the date to the file.

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TEST : MonoBehaviour
{
    string name_file;

    private void Start()
    {
        name_file = Path.Combine(Application.dataPath, "SaveDate.json");

        if (File.Exists(name_file))
        {
            Debug.Log(JsonUtility.FromJson<DateTime>(File.ReadAllText(name_file)));
        }
    }

    public void DataSave()
    {
        Debug.Log(DateTime.Now);
        File.WriteAllText(name_file, JsonUtility.ToJson(DateTime.Now));
    }

    public void Delete()
    {
        File.Delete(name_file);
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-03-10
@Sterio23400

I would not try to serialize in json fig understand what DateTime structure. Apparently it cannot be reverse engineered.
Use a timestamp - it's an int (for now) - it won't take up much space, converting back and forth is great.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question