ReadJosn.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. public class ReadJosn
  7. {
  8. /// <summary>
  9. /// 通过文件解析json
  10. /// </summary>
  11. /// <param name="fileName"></param>
  12. /// <returns></returns>
  13. public static JsonExample LoadJsonFromFile(string fileName)
  14. {
  15. if (Application.platform == RuntimePlatform.WindowsEditor
  16. || Application.platform == RuntimePlatform.WindowsPlayer)
  17. {
  18. if (!File.Exists(Application.dataPath + "/StreamingAssets/Setting/" + fileName))
  19. {
  20. return null;
  21. }
  22. StreamReader sr = new StreamReader(Application.dataPath + "/StreamingAssets/Setting/" + fileName);
  23. if (sr == null)
  24. {
  25. return null;
  26. }
  27. string json = sr.ReadToEnd();
  28. if (json.Length > 0)
  29. {
  30. return JsonUtility.FromJson<JsonExample>(json);
  31. }
  32. return null;
  33. }
  34. //if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
  35. //{
  36. // if (!File.Exists(Application.dataPath + "/storage/emulated/0/Android/data/com.zwn.VirusEnd2/files/Setting/" + fileName))
  37. // {
  38. // return null;
  39. // }
  40. // StreamReader sr = new StreamReader(Application.dataPath + "/storage/emulated/0/Android/data/com.zwn.VirusEnd2/files/Setting/" + fileName);
  41. // if (sr == null)
  42. // {
  43. // return null;
  44. // }
  45. // string json = sr.ReadToEnd();
  46. // if (json.Length > 0)
  47. // {
  48. // return JsonUtility.FromJson<JsonExample>(json);
  49. // }
  50. // return null;
  51. //}
  52. return null;
  53. }
  54. public static T LoadJson<T>(string json)
  55. {
  56. T jsonExample = JsonUtility.FromJson<T>(json);
  57. return jsonExample;
  58. }
  59. }