怎样实现unity读取本地json文件呢?现在我们就根据一篇文章来看一下unity读取本地json文件的具体过程。
1,在unity3d 工程中创建一个Plugs文件夹,将网上下好的LitJson1.1放在这个文件夹里。
2,再创建一个Resources文件夹,将json文本“mytestui.txt”放在这个文件夹。
3,mytestui.txt 的内容为:--------------------------------mytestui.txt-------------------
- [csharp] view plaincopy
- {
- "frames":
- {
- "BarGreyBlue2.png":
- {
- "frame": {"x":2,"y":2,"w":3,"h":88},
- "rotated": false,
- "trimmed": false,
- "spriteSourceSize": {"x":0,"y":0,"w":3,"h":88},
- "sourceSize": {"w":3,"h":88}
- },
- "blueBg2.png":
- {
- "frame": {"x":2,"y":92,"w":14,"h":14},
- "rotated": false,
- "trimmed": false,
- "spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
- "sourceSize": {"w":14,"h":14}
- }
- }
- }
- [csharp] view plaincopy
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using LitJson;
- public class myframe
- {
- public int x;
- public int y;
- public int w;
- public int h;
- }
- public class mySpriteSourceSize
- {
- public int x;
- public int y;
- public int w;
- public int h;
- }
- public class mySourceSize
- {
- public int w;
- public int h;
- }
- public class myPng
- {
- public mySourceSize sourceSize;
- public mySpriteSourceSize spriteSourceSize;
- public myframe frame;
- public bool rotated;
- public bool trimmed;
- }
- public class myPngs
- {
- public Dictionary<string, myPng> frames;
- }
- public class test : MonoBehaviour
- {
- void Start()
- {
- var textObj = Resources.Load("mytestui") as TextAsset;
- myPngs pngs = JsonMapper.ToObject<myPngs>(textObj.text);
- foreach (var i in pngs.frames)
- {
- print(i.Key + " " + i.Value.rotated);
- }
- }
- }
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)