注:!!!untiy 打包安卓平台不允许使用Unity自带的json工具Newtonsoft.Json
LitJson不支持float类型的字符。
LitJson支持的格式如下:
public JsonData(bool boolean);
public JsonData(double number);
public JsonData(int number);
public JsonData(long number);
public JsonData(object obj)
public JsonData(string str);
需要让LitJson支持Float类型的方式:
在Awake里面或者在初始化协程里面调用方法即可
/// <summary>
/// LitJson数据转换,可使用Float
/// </summary>
private static void RegisterFloat()
{
void Exporter(float obj, JsonWriter writer)
{
writer.Write(obj);
}JsonMapper.RegisterExporter((ExporterFunc<float>)Exporter);
float Importer(double obj)
{
return (float)obj;
}JsonMapper.RegisterImporter((ImporterFunc<double, float>)Importer);
}
安卓端,IOS,PC的JSON转换(通用)
using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Networking;public class JsonManager : SingleMono<JsonManager>
{public AllShengLingManagers allShengLing; //生灵列表public string path; //Json存放路径void Start(){//初始化JsonStartCoroutine(LoadSave("人族"));}void Update(){if (Input.GetKeyDown(KeyCode.V)){for (int i = 0; i < allShengLing.ShengLingList.Count; i++){Debug.Log(allShengLing.ShengLingList[i].name);}}}/// <summary>/// 初始化人物操作/// </summary>/// <param name="name">种族名称</param>/// <returns></returns>public IEnumerator LoadSave(string name){RegisterFloat(); //可使用Float类型数据path = Application.persistentDataPath +"/"+ "AllShengLing.json"; //allShengLing = new AllShengLingManagers();//初始化生灵//allShengLing.ShengLingList = new List<ShengLing>(); //初始化UnityWebRequest www = UnityWebRequest.Get("file://" + path); //获取路径yield return www.SendWebRequest(); //发生请求if (www.result == UnityWebRequest.Result.Success){var fileJson = www.downloadHandler.text; //读取JSON文件Debug.Log(fileJson);Debug.Log($"数据存在---");StreamReader r = new StreamReader(path); // 读string srRTE = r.ReadToEnd();Debug.Log("<color=#ff7f00>" + "Json配置文件内容:" + "</color>" + srRTE);allShengLing = JsonMapper.ToObject<AllShengLingManagers>(srRTE); //反序列化生灵列表r.Close(); //关闭文本if (allShengLing == null){Debug.Log("Json为空");}else //有数据的话,把各个种族的数据初始化{for (int i = 0; i < allShengLing.ShengLingList.Count; i++){if (CSVTest.instance.names.Contains(allShengLing.ShengLingList[i].name)) //判断名字是否相等,然后给到值,初始化{//某个UI等于该数据的数值}} //for (int i = 0; i < CSVTest.instance.names.Count; i++) //查找names里面有没有这个名字//{// if (CSVTest.instance.names[i].Equals(name)) //循环遍历列表里面的名字是不是叫name,是的情况下就加成并退出循环// {// allShengLing.ShengLingList[i].AllZhanLi += 10000; //总战力+10000// allShengLing.ShengLingList[i].AllSum += 10000; //总人数+10000// break;// }//} }}else{Debug.Log("没有该生灵的Json文件,正在生成该数据");//初始化生灵数据Init(name);}}/// <summary>/// 初始化生灵/// </summary>public void Init(string name){ShengLing sl = new ShengLing();sl.name = name;sl.LeveL = 1;sl.QiangZhe = 0;sl.QiWu = 0;sl.MiFa = 0;sl.XieMai = 0;sl.AllSum = 10000;sl.AllZhanLi = 10000;allShengLing.ShengLingList.Add(sl);CSVTest.instance.names.Add("人族");GameManagers.instance.SaveRoleData(allShengLing, "AllShengLing.json");}/// <summary>/// LitJson数据转换,可使用Float/// </summary>public void RegisterFloat(){void Exporter(float obj, JsonWriter writer){writer.Write(obj);}JsonMapper.RegisterExporter((ExporterFunc<float>)Exporter);float Importer(double obj){return (float)obj;}JsonMapper.RegisterImporter((ImporterFunc<double, float>)Importer);}/// <summary>/// 删除对象/// </summary>/// <param name="name">要删除的名字</param>public void RemoveShuJu(string name){for (int i = 0; i < allShengLing.ShengLingList.Count; i++){var item = allShengLing.ShengLingList[i]; //要删除的对象if (item.name.Equals(name)){allShengLing.ShengLingList.Remove(item);GameManagers.instance.SaveRoleData(allShengLing, "AllShengLing.json");}}}}
保存JSON脚本展示,放入单例脚本中,方便调用
/// <summary>/// Json保存/// </summary>/// <typeparam name="T">数据类型</typeparam>/// <param name="data">对象</param>public void SaveRoleData<T>(T data,string name){Debug.Log("数据保存");string path = Application.persistentDataPath + "/"+ name;string saveJsonStr = JsonMapper.ToJson(data); //序列化JSON文件,写入JsonFile.WriteAllText(path, saveJsonStr);}