赞
踩
目录
unity提供的现成可用的本地存储方式:
- PlayerPrefs.SetFloat("size", 1.5f);
- PlayerPrefs.SetInt("age", 10);
- PlayerPrefs.SetString("name", "David");
-
- using UnityEngine;
-
- public class Entity_fwy
- {
-
- public enum_fwy state;
-
- public string name_fwy = "";
-
- public int age;
-
- }
- using System.Collections;
- using System.Collections.Generic;
- using Newtonsoft.Json;
- using UnityEngine;
- using static Pp_enum;
-
- public class Contr_fwy : MonoBehaviour
- {
-
- void Start()
- {
-
- Entity_fwy entityFwy = new Entity_fwy();
- entityFwy.state = enum_fwy.idle;
- entityFwy.name_fwy = "XiaoBai";
- entityFwy.age = 18;
-
- //存储
- PlayerPrefs.SetString("fwy", JsonConvert.SerializeObject(entityFwy));
-
- //获取
- Entity_fwy enFwy = JsonConvert.DeserializeObject<Entity_fwy>(PlayerPrefs.GetString("fwy", ""));
- Debug.LogError("cc:" + enFwy.name_fwy);
-
- }
-
-
- }
- public class DataSave
- {
-
- public string obj_name;
-
- public int obj_id;
- public Vector3 obj_pos;
- public int obj_scale;
- }
- /// <summary>
- /// list 转json
- /// </summary>
- public static string ToJson<T>(this T list)
- {
- JsonSerializerSettings settings = new JsonSerializerSettings();
- settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
- return JsonConvert.SerializeObject(list, settings);
- }
-
- [HideInInspector]
- public List<DataSave> list_sbHave = new List<DataSave>();
-
- public void dataSave( string str)
- {
- switch(str){
- case "sb":
- //string str = MyUtil.ToJson(list_dataHave_gt);
- string str = list_sbHave.ToJson();
- Debug.LogError("save:" + str);
- PlayerPrefs.SetString("have_sb", str);
- break;
- ......
- }
-
- public void DataGet()
- {
- string sb_str = PlayerPrefs.GetString("have_sb", "");
- if (string.IsNullOrEmpty(sb_str))
- {
- //默认拥有柜台
- DataSheBei ds1 = new DataSheBei
- {
- obj_name = "gt",
- obj_id = 12,
- obj_pos = new Vector3(-0.02f, 1.99f, -5),
- obj_scale = 1
- };
- PlayerPrefs.SetInt("barIndex", 0);
- list_sbHave.Add(ds1);
- //默认拥有1个四人桌
- DataSheBei ds2 = new DataSheBei
- {
- obj_name = "zy",
- obj_id = 1,
- obj_pos = new Vector3(-2.32f, 0.815f, -5),
- obj_scale = 1
- };
- PlayerPrefs.SetInt("tableIndex", 0);
- list_sbHave.Add(ds2);
- }
- else
- {
- JArray ja = (JArray)JsonConvert.DeserializeObject(sb_str);
- Debug.LogError("sb:" + ja.Count);
- for (int i = 0; i < ja.Count; i++)
- {
- DataSheBei ds = new DataSheBei();
- ds.obj_name = ja[i]["obj_name"].ToString();
- ds.obj_id = int.Parse(ja[i]["obj_id"].ToString());
-
- JObject jo = JObject.Parse(ja[i]["obj_pos"].ToString());
- ds.obj_pos = new Vector3(float.Parse(jo["x"].ToString()), float.Parse(jo["y"].ToString()), -5);
- ds.obj_scale = int.Parse(ja[i]["obj_scale"].ToString());
- list_sbHave.Add(ds);
- }
- }
-
- InitSbHave();
- ......
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。