| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- public class ReadJosn
- {
- /// <summary>
- /// 通过文件解析json
- /// </summary>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static JsonExample LoadJsonFromFile(string fileName)
- {
- if (Application.platform == RuntimePlatform.WindowsEditor
- || Application.platform == RuntimePlatform.WindowsPlayer)
- {
- if (!File.Exists(Application.dataPath + "/StreamingAssets/Setting/" + fileName))
- {
- return null;
- }
- StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets/Setting/" + fileName);
- if (sr == null)
- {
- return null;
- }
- string json = sr.ReadToEnd();
- if (json.Length > 0)
- {
- return JsonUtility.FromJson<JsonExample>(json);
- }
- return null;
- }
- //if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
- //{
- // if (!File.Exists(Application.dataPath + "/storage/emulated/0/Android/data/com.zwn.VirusEnd2/files/Setting/" + fileName))
- // {
- // return null;
- // }
- // StreamReader sr = new StreamReader(Application.dataPath + "/storage/emulated/0/Android/data/com.zwn.VirusEnd2/files/Setting/" + fileName);
- // if (sr == null)
- // {
- // return null;
- // }
- // string json = sr.ReadToEnd();
- // if (json.Length > 0)
- // {
- // return JsonUtility.FromJson<JsonExample>(json);
- // }
- // return null;
- //}
- return null;
- }
- public static T LoadJson<T>(string json)
- {
- T jsonExample = JsonUtility.FromJson<T>(json);
- return jsonExample;
- }
- }
|