public class MainGame : MonoBehaviour
{
public List<PlanetaryData> m_AllPlanetaryData = new List<PlanetaryData>();
private void Start()
{
m_AllPlanetaryData = JsonLoad.Instance.LoadJsonDatas<List<PlanetaryData>>(Application.streamingAssetsPath + "/Json/data.json");
for (int i = 0; i < m_AllPlanetaryData.Count; i++)
{
Debug.Log(m_AllPlanetaryData[i].m_Name);
}
}
}
///
/// 地球数据简介
///
[System.Serializable]
public class PlanetaryData
{
///
/// 名字
///
public string m_Name { get; set; }
///
/// 英文名字
///
public string m_EnglishName { get; set; }
///
/// 简介
///
public string m_Hint { get; set; }
///
/// 矿产
///
public string m_MineralProducts { get; set; }
}
JSON
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using System;
using System.Text;
public class JsonLoad
{
private static JsonLoad instance;
public static JsonLoad Instance
{
get
{
if (instance == null)
{
instance = new JsonLoad();
}
return instance;
}
}
///
/// 读取0
///
///
///
///
public T LoadJsonDatas<T>(string path)
{
StreamReader sr = new StreamReader(path);
string readContent = sr.ReadToEnd();
T jsonDatas = JsonMapper.ToObject<T>(readContent);
sr.Close();
sr.Dispose();
return jsonDatas;
}
///
/// 读取1
///
///
///
///
public T LoadFromJsonDate<T>(string UnityAssetSubPath)
{
try
{
string filePath = Application.dataPath + UnityAssetSubPath;
T t = JsonMapper.ToObject<T>(File.ReadAllText(filePath));
if (t == null)
{
Debug.Log("JsonDate=Null");
return default(T);
}
return t;
}
catch (Exception E)
{
Debug.LogError(E);
return default(T);
}
}
///
/// 保存
///
///
///
///
///
public string SaveJsonDate<T>(T t, string UnityAssetSubPath)
{
string filePath = Application.dataPath + UnityAssetSubPath;
FileInfo file = new FileInfo(filePath);
string jsonstr = JsonMapper.ToJson(t);
using (System.IO.StreamWriter thefile = new StreamWriter(filePath, false, Encoding.UTF8))
{
thefile.WriteLine(jsonstr);
}
//在编辑器下
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
return jsonstr;
}
}
public class JsonDatas
{
///
/// 地球
///
public Star Earth { get; set; }
public Star Port { get; set; }
}
///
/// 星
///
public class Star
{
public string TextIntr { get; set; }
public string crystal { get; set; }
}