亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

加載我的數組時,值是相同的(Unity)

加載我的數組時,值是相同的(Unity)

PHP
揚帆大魚 2024-01-20 21:11:28
我剛剛為我的游戲重新制作了保存系統。為此,我使用 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"));        }這是執行 UI 的加載操作。 public void AddProfiles()    {        ProfilesData data = SaveProfiles.LoadProfiles();        img = data.profileImage;        ProfilesUIClone = Instantiate(ProfilesUI) as GameObject;        ProfilesUIClone.transform.SetParent(wheretomaketheprofileUI.transform.parent);        Profile = ProfilesUIClone.transform.Find("SelectProfile").transform.Find("Name").GetComponentInChildren<Text>();        Company = ProfilesUIClone.transform.Find("SelectProfile").transform.Find("Company").GetComponentInChildren<Text>();        Profile.text = data.profileName;        Company.text = data.companyName;        ProfilesUIClone.SetActive(true);        if (img == 0)        {            Image = ProfilesUIClone.transform.Find("SelectProfile").transform.Find("ProfileImage1").GetComponent<Image>();            Image.gameObject.SetActive(true);        }        else if (img == 1)        {            Image = ProfilesUIClone.transform.Find("SelectProfile").transform.Find("ProfileImage2").GetComponent<Image>();            Image.gameObject.SetActive(true);        }        else if (img == 2)        {            Image = ProfilesUIClone.transform.Find("SelectProfile").transform.Find("ProfileImage3").GetComponent<Image>();            Image.gameObject.SetActive(true);        }    }我希望它會按應有的方式加載所有不同的配置文件,但事實并非如此,它只是加載其中一個值并將其應用于所有配置文件。
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

您正在覆蓋您的數據。另外,(也許)不要為此使用 PlayerPrefs。

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"));

您會注意到它總是打印一件事。最后的個人資料。


如果您堅持讓 PlayerPrefs 做一些它從未設計過的事情,那么您必須將鍵名稱 ( "Path") 更改為更獨特的名稱 ( "Path"+i) 或以其他方式確定您希望在那里存儲哪個配置文件。由于我不清楚你的用例,所以你必須自己弄清楚。


查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號