有很多方法可以解决。json XML都行。小白用了最简单unity自带的方法解决。下面贴上代码 有创建,删除,读取,存储。 还有具体用法。比较乱SaveData脚本可以直接拿到项目中使用。
using System;
using com.QH.QPGame.GameUtils;
using com.QH.QPGame.Utility;
using UnityEngine;
namespace com.QH.QPGame.Lobby
{
/// <summary>
/// 设置管理类,保存读取设置,一般的只保存玩家客户端临时数据
/// @Author: guofeng
/// </summary>
public class GameSettings
{
public void Load()
{
string account = PlayerPrefs.GetString(GlobalConst.Settings.Account);
string password = PlayerPrefs.GetString(GlobalConst.Settings.Password);
string saveword = PlayerPrefs.GetString(GlobalConst.Settings.SavePass);
//string version = PlayerPrefs.GetString(GlobalConst.Settings.Version);
string installVersion = PlayerPrefs.GetString(GlobalConst.Settings.InstallVersion);
GameApp.GameData.Account = account;
GameApp.GameData.Password = password;
GameApp.GameData.InstallVersion = installVersion;
bool.TryParse(saveword, out GameApp.GameData.SavePassword);
}
public void Save()
{
PlayerPrefs.SetString(GlobalConst.Settings.Account, GameApp.GameData.Account);
PlayerPrefs.SetString(GlobalConst.Settings.SavePass, GameApp.GameData.SavePassword.ToString());
//PlayerPrefs.SetString(GlobalConst.Settings.Version, GameApp.GameData.Version);
PlayerPrefs.SetString(GlobalConst.Settings.InstallVersion, GameApp.GameData.Version);
if (GameApp.GameData.SavePassword)
{
PlayerPrefs.SetString(GlobalConst.Settings.Password, GameApp.GameData.Password);
}
else
{
PlayerPrefs.DeleteKey(GlobalConst.Settings.Password);
}
}
public void SaveUserInfo()
{
SaveDate.creatData(GameApp.GameData.Account, GameApp.GameData.Password);
}
public void SwitchUser(int i)
{
var arrData = SaveDate.readData();
GameApp.GameData.Account = arrData[i]["AccountName"];
GameApp.GameData.Password = arrData[i]["AccountPassword"];
}
}
}
using UnityEngine;
using System.Collections;
using com.QH.QPGame.Lobby;
public class SwitchUser : MonoBehaviour {
public UILabel[] labelList;
public GameObject[] userFrame;
void Start()
{
}
void OnEnable()
{
Refresh();
}
void Update()
{
}
public void Refresh()
{
if (SaveDate.readData() != null)
{
for (int i = 0; i < SaveDate.readData().Count; i++)
{
labelList[i].text = SaveDate.readData()[i]["AccountName"];
userFrame[i].transform.localScale = Vector3.one;
}
}
}
public void DesCommon(int num)
{
SaveDate.deleteData(num);
userFrame[SaveDate.readData().Count].transform.localScale = Vector3.zero;
Refresh();
GameApp.GameData.Account = "";
GameApp.GameData.Password = "";
}
public void Login()
{
GameApp.Account.SendLoginHallSvr(
GameApp.GameData.Account,
GameApp.GameData.Password,
GameApp.GameData.MAC
);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SaveDate : MonoBehaviour
{
public static void deleteData (int index)
{
List<Dictionary<string,string>> arr = readData ();
arr.RemoveAt (index);
saveData (arr);
}
public static void creatData (string account, string password)
{
List<Dictionary<string,string>> arr = readData ();
Dictionary<string,string> dic = new Dictionary<string, string> ();
for (int i = 0; i < arr.Count; i++) {
if (account == arr [i] ["AccountName"]) {
dic = arr [i];
//arr.Remove (arr [i]);
}
}
if (dic.Count > 0) {
arr.Remove (dic);
}
Dictionary<string, string> openWith = new Dictionary<string, string> ();
openWith.Add ("AccountName", account);
openWith.Add ("AccountPassword", password);
List<Dictionary<string, string>> arr1 = new List<Dictionary<string, string>>();
arr1.Add(openWith);
foreach ( var obj in arr) {
arr1.Add(obj);
}
saveData(arr1);
}
public static List<Dictionary<string,string>> readData ()
{
List<Dictionary<string,string>> arr = new List<Dictionary<string, string>> ();
for (int i = 0; i < 5; i++) {
string acc = PlayerPrefs.GetString ("AccountName" + i, null);
string pass = PlayerPrefs.GetString ("AccountPassword" + i, null);
if (acc.Length>0) {
Dictionary<string, string> openWith = new Dictionary<string, string> ();
openWith.Add ("AccountName", acc);
openWith.Add ("AccountPassword", pass);
arr.Add (openWith);
}
}
return arr;
}
public static void saveData (List<Dictionary<string,string>> arr)
{
for (int k = 0; k < 5; k++) {
if (k < arr.Count && arr[k]["AccountName"].Length>0) {
PlayerPrefs.SetString ("AccountName" + k, arr [k] ["AccountName"]);
PlayerPrefs.SetString ("AccountPassword" + k, arr [k] ["AccountPassword"]);
Debug.Log(arr[k]["AccountName"] + "......" + arr[k]["AccountPassword"]);
} else {
PlayerPrefs.DeleteKey ("AccountName" + k);
PlayerPrefs.DeleteKey ("AccountPassword" + k);
}
}
}
}