public static string GetString(string stringKey) { if (stringKey == string.Empty) { throw new Exception("intKey不能为空"); } foreach (var item in playerPrefsStringList) { PlayerPrefsStringModel model = new PlayerPrefsStringModel(); try { model = Coding <PlayerPrefsStringModel> .decode(item); } catch (Exception err) { Debug.Print("GetString 解析失败" + err.ToString()); return(""); } if (model.stringKey == stringKey) //找到了数据 { return(model.stringValue); } } return(""); }
public static void SetString(string stringKey, string stringValue) { if (stringKey == string.Empty) { throw new Exception("stringKey不能为空"); } bool isAllreadyExist = false; for (int i = 0; i < playerPrefsStringList.Count; i++) { // Debug.Print("每一条PlayerPrefs数据:" + playerPrefsList[i]); PlayerPrefsStringModel model = new PlayerPrefsStringModel(); try { model = Coding <PlayerPrefsStringModel> .decode(playerPrefsStringList[i]); } catch (Exception err) { Debug.Print("SetString解析失败" + err.ToString()); return; } if (model.stringKey == stringKey) //找到了数据 { isAllreadyExist = true; PlayerPrefsStringModel stringModel = new PlayerPrefsStringModel(); stringModel.stringKey = stringKey; stringModel.stringValue = stringValue; string updateItem = Coding <PlayerPrefsStringModel> .encode(stringModel); playerPrefsStringList[i] = updateItem; break; } } if (isAllreadyExist == false) { PlayerPrefsStringModel stringModel = new PlayerPrefsStringModel(); stringModel.stringKey = stringKey; stringModel.stringValue = stringValue; string updateItem = Coding <PlayerPrefsStringModel> .encode(stringModel); playerPrefsStringList.Add(updateItem); } //写入新文件 using (StreamWriter file = new System.IO.StreamWriter(PlayerPrefsStringPath, false)) { string newJsonFile = ""; foreach (var item in playerPrefsStringList) { newJsonFile += item + "\r"; } file.WriteLine(newJsonFile); // 直接追加文件末尾,换行 } }