我的保存/加载系统不起作用。目录从未被创建。

huangapple go评论62阅读模式
英文:

My Save/Load System doesn't work. The directory is never created

问题

我有一个用于加载和保存数据的静态泛型类。我没有任何编译错误,但系统不保存或加载。我的项目在Unity中。

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

public static class DataManager
{
    public static void Save<T>(T data, string path, string name)
    {
        path = Application.persistentDataPath + "/SaveData" + path;

        if (!Directory.Exists(path))
        {
            Debug.Log("Create New Directory");
            Directory.CreateDirectory(path);
        }

        path = path + "/" + name + ".txt";

        Debug.Log("Save: " + path);
        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T Load<T>(string path, string name) where T : Savable, new()
    {
        path = Application.persistentDataPath + "/SaveData" + path + "/" + name + ".txt";

        Debug.Log("Load: " + path);
        T data = new T();

        if (Directory.Exists(path))
        {
            Debug.Log("Data Exist (Load)");
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson<T>(json);
        }
        else
        {
            Debug.Log("Data Don't Exist (Load)");
            data.Name = name;
        }

        return data;
    }
}

我在程序中添加了几个Debug.Log()来查找问题,但是没有发现任何问题。我还在保存之前和保存之后添加了一个Debug.Log(),显示其中包含的数据。

根据日志中的信息,如果我们考虑没有任何数据,第一次加载是正常的(对象名称之后的值是对象中的值)。

当保存发生时,对象包含修改后的值,路径相同。它找不到任何目录,所以它创建一个新目录。

当我第二次加载时,它找不到数据并再次使用默认数据。

我需要数据能够正确加载。

英文:

I have a static generic class which is used to load and save Data. I don't have any compilation error, but the system doesn't save or load. My project is on Unity.

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

public static class DataManager
{
    public static void Save&lt;T&gt;(T data, string path, string name)
    {

        path = Application.persistentDataPath + &quot;/SaveData&quot; + path;

        if (!Directory.Exists(path))
        {
            Debug.Log(&quot;Create New Directory&quot;);
            Directory.CreateDirectory(path);
        }

        path = path + &quot;/&quot; + name + &quot;.txt&quot;;

        Debug.Log(&quot;Save : &quot;+path);
        string json = JsonUtility.ToJson(data, true);

        File.WriteAllText(path, json);
    }

    public static T Load&lt;T&gt;(string path, string name) where T : Savable, new()
    {
        path = Application.persistentDataPath + &quot;/SaveData&quot; + path + &quot;/&quot; + name + &quot;.txt&quot;;

        Debug.Log(&quot;Load : &quot;+path);
        T data = new T();

        if (Directory.Exists(path))
        {
            Debug.Log(&quot;Data Exist (Load)&quot;);
            string json = File.ReadAllText(path);

            data = JsonUtility.FromJson&lt;T&gt;(json);
        }
        else
        {
            Debug.Log(&quot;Data Don&#39;t Exist (Load)&quot;);
            data.Name = name;
        }

        return data;
    }
}

I put several Debug.Log() in the programme to find the problem, but I don't find anything.
I als put a Debug.Log() just before the save and just after the save with the data it contain.

Debug.Log

As I saw in the Log, the first load is normal if we consider we don't have any data. (The value after the name of the object are the value in the object.)

When the save happen the object possess the value after modification and the path is the same. It doesn't find any directory so it create a new directory.

When I load a second time, it doesn't find the data and use the default data again.

I need the data to be able to load properly.

答案1

得分: 2

因为在Load方法中,path是文件路径,但你正在使用Directory.Exists来检查文件是否存在,它将始终返回false,你应该使用File.Exists代替。

英文:

Because in the Load method path is a file path but you are using Directory.Exists to check if the file exists, it will always return false, you should use File.Exists instead.

huangapple
  • 本文由 发表于 2023年6月16日 12:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486959.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定