怎样实现unity读取本地json文件呢?现在我们就根据一篇文章来看一下unity读取本地json文件的具体过程。

1,在unity3d 工程中创建一个Plugs文件夹,将网上下好的LitJson1.1放在这个文件夹里。

2,再创建一个Resources文件夹,将json文本“mytestui.txt”放在这个文件夹。

3,mytestui.txt 的内容为:
--------------------------------mytestui.txt-------------------
  1. [csharp] view plaincopy
  2. {
  3. "frames":
  4. {
  5. "BarGreyBlue2.png":
  6. {
  7. "frame": {"x":2,"y":2,"w":3,"h":88},
  8. "rotated": false,
  9. "trimmed": false,
  10. "spriteSourceSize": {"x":0,"y":0,"w":3,"h":88},
  11. "sourceSize": {"w":3,"h":88}
  12. },
  13. "blueBg2.png":
  14. {
  15. "frame": {"x":2,"y":92,"w":14,"h":14},
  16. "rotated": false,
  17. "trimmed": false,
  18. "spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
  19. "sourceSize": {"w":14,"h":14}
  20. }
  21. }
  22. }
-------------------------------调用----------test.cs-----------------
  1. [csharp] view plaincopy
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using LitJson;
  6. public class myframe
  7. {
  8. public int x;
  9. public int y;
  10. public int w;
  11. public int h;
  12. }
  13. public class mySpriteSourceSize
  14. {
  15. public int x;
  16. public int y;
  17. public int w;
  18. public int h;
  19. }
  20. public class mySourceSize
  21. {
  22. public int w;
  23. public int h;
  24. }
  25. public class myPng
  26. {
  27. public mySourceSize sourceSize;
  28. public mySpriteSourceSize spriteSourceSize;
  29. public myframe frame;
  30. public bool rotated;
  31. public bool trimmed;
  32. }
  33. public class myPngs
  34. {
  35. public Dictionary<string, myPng> frames;
  36. }
  37. public class test : MonoBehaviour
  38. {
  39. void Start()
  40. {
  41. var textObj =  Resources.Load("mytestui") as TextAsset;
  42. myPngs pngs = JsonMapper.ToObject<myPngs>(textObj.text);
  43. foreach (var i in pngs.frames)
  44. {
  45. print(i.Key + " " + i.Value.rotated);
  46. }
  47. }
  48. }
-----------------打印-------------------
BarGreyBlue2.png False
UnityEngine.MonoBehaviour:print(Object)
test:Start() (at Assets/test.cs:56)
blueBg2.png False
UnityEngine.MonoBehaviour:print(Object)
test:Start() (at Assets/test.cs:56)