Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载数组时,值完全相同(单位)_C#_Unity3d - Fatal编程技术网

C# 加载数组时,值完全相同(单位)

C# 加载数组时,值完全相同(单位),c#,unity3d,C#,Unity3d,我只是为我的游戏重新设计了我的储蓄系统。为此,我使用Directory.GetDirectories和一个数组来存储它。问题是,当加载时,加载代码只加载相同的文件夹数量值。string[]profiles数组不同,但它加载的内容不同(ProfilesData) 我也试着用谷歌搜索它,但我想我搜索到了一些错误的东西。答案很有可能是答案,但我不确定要搜索什么。我已经尝试过自己调试它了。我可以看到,当它被加载时,“值”是相同的,但我不知道为什么 这是第一个得到数组的脚本,它应该会改变路径 //Get

我只是为我的游戏重新设计了我的储蓄系统。为此,我使用Directory.GetDirectories和一个数组来存储它。问题是,当加载时,加载代码只加载相同的文件夹数量值。
string[]profiles
数组不同,但它加载的内容不同(
ProfilesData


我也试着用谷歌搜索它,但我想我搜索到了一些错误的东西。答案很有可能是答案,但我不确定要搜索什么。我已经尝试过自己调试它了。我可以看到,当它被加载时,“值”是相同的,但我不知道为什么

这是第一个得到数组的脚本,它应该会改变路径

 //Gets all Directories in the Profiles folder
        string[] profiles = Directory.GetDirectories(Application.persistentDataPath + "/Profiles/");

        for (int i = 0; i < profiles.Length; i++)
        {
            PlayerPrefs.SetString("Path", profiles.GetValue(i).ToString());
            AddProfiles();
            Debug.Log(PlayerPrefs.GetString("Path"));
        }
我希望它会加载所有不同的配置文件,但它不会加载其中一个值,并将其用于所有配置文件。

您正在覆盖数据。另外,(可能)不要为此使用PlayerPrefs。 每次通过循环时,都将同一个键设置为新值。如果您要这样做:

    for (int i = 0; i < profiles.Length; i++)
    {
        PlayerPrefs.SetString("Path", profiles.GetValue(i).ToString());
        AddProfiles();
    }
    Debug.Log(PlayerPrefs.GetString("Path"));
for(int i=0;i
你会注意到它总是打印一件东西。最后一个配置文件


如果你坚持让PlayerPref做一些它从来没有设计过的事情,那么你必须将键名(
“Path”
)更改为更独特的(
“Path”+i
)或者决定你希望存储在那里的一个。因为我不清楚您的用例,所以您必须自己解决这个问题。

“我可以看到,当它被加载时,“值”是相同的,但我不知道为什么。”。这个值是多少?您还可以尝试用注释来记录代码,使其更具可读性吗?奇怪的是,您设置了“Path”的首选项,但通过for循环多次更改它。另外,您是说“ProfileData”加载不正确吗?(因此错误在于LoadProfiles())我会在可能的时候添加注释,谢谢回复。在创建概要文件时,加载功能可以工作,但在加载概要文件时,它不能正常工作。我之所以循环,是因为不同的配置文件有不同的路径。你是说加载功能可以工作,但加载时不起作用?您是否在谈论不同的事情,您是否可以更具体一点,并提及特定的函数/变量?(例如“LoadProfiles()”或“ProfileData”)谢谢。哦,是的,对不起,我的意思是LoadProfiles()在您创建配置文件时起作用,但当您转到主菜单并加载它们时,它确实会加载,但所有UI元素的值都相同,但应该不同。我的意思是LoadProfiles()和else if(File.Exists(path2))用于加载主菜单。为所有菜单UI加载的值是否与特定配置文件的值相似?(例如,始终是最后一个配置文件或第一个配置文件)。您还可以演示如何定义ProfilesUIClone和ProfilesUI,以及最后,何时调用上述脚本?一旦开始?好的,是的playerprefs不是最好的,我只是用它来测试,并把它从那个文件转移到另一个文件,谢谢你的回复,我会研究它。用例是,它将看到Profiles文件夹中有多少文件夹,然后它将加载其中有多少文件夹,这一部分是正确的,只是UI为所有文件夹获得了相同的值,但应该是不同的,通过一个示例可以吗?谢谢。
AddProfiles()
当前添加了一个配置文件,而不是所有配置文件。它通过调用
LoadProfiles()
(同样,只有一个)来实现,该函数通过查询
PlayerPrefs
来实现。是否改为使用参数<代码>AddProfile(pathprofiles.GetValue(i.ToString())实际上没有理由使用“全局”存储系统来传递一些数据。好的,谢谢,我非常感谢,我会进行测试。
 public static ProfilesData LoadProfiles()
    {
        string path = Application.persistentDataPath + "/Profiles/" + PlayerPrefs.GetString("ProfileName") + "/config.dat";
        string path2 = PlayerPrefs.GetString("Path");
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            ProfilesData data = formatter.Deserialize(stream) as ProfilesData;
            stream.Close();

            return data;
        }
        else if (File.Exists(path2))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path2, FileMode.Open);

            ProfilesData data = formatter.Deserialize(stream) as ProfilesData;
            stream.Close();

            return data;
        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        }
    }
PlayerPrefs.SetString("Path", profiles.GetValue(i).ToString());
    for (int i = 0; i < profiles.Length; i++)
    {
        PlayerPrefs.SetString("Path", profiles.GetValue(i).ToString());
        AddProfiles();
    }
    Debug.Log(PlayerPrefs.GetString("Path"));