Unity3D——LitJson解析json,读取关卡信息
发表于2018-09-13
Litjson是用C #编写的,它的目的是要小,快速,易用,而json又是功能强大的序列化数据格式,下面要分享的是通过LitJson解析json来读取关卡信息的教程。
json文件:LevelInfo.txt
{ "levelList": [ { "Number": 1, "IsLock": 0, "Star": 0, "Timer": 30, "TargetScore": 4 }, { "Number": 2, "IsLock": 1, "Star": 0, "Timer": 29, "TargetScore": 4 }, { "Number": 3, "IsLock": 1, "Star": 0, "Timer": 28, "TargetScore": 6 }, { "Number": 4, "IsLock": 1, "Star": 0, "Timer": 27, "TargetScore": 6 }, { "Number": 5, "IsLock": 1, "Star": 0, "Timer": 26, "TargetScore": 8 }, { "Number": 6, "IsLock": 1, "Star": 0, "Timer": 25, "TargetScore": 8 }, { "Number": 7, "IsLock": 1, "Star": 0, "Timer": 24, "TargetScore": 9 }, { "Number": 8, "IsLock": 1, "Star": 0, "Timer": 23, "TargetScore": 10 } ] }
Unity中C#脚本:LevelJsonPrase.cs
using UnityEngine; using System.Collections; using System.Collections.Generic; using LitJson; public class LevelJsonPrase : MonoBehaviour { #region class [System.Serializable] class Level { public int Number = 0; public int IsLock = 0; public int Star = 0; public int TargetScore = 0; public float Timer = 0f; } [System.Serializable] class LevelList { public List<Level> levelList = new List<Level>(); } #endregion #region variables [SerializeField] private TextAsset textJson; [SerializeField] private LevelList listInfo = null; #endregion // Use this for initialization void Start () { textJson = Resources.Load("LevelInfo") as TextAsset; listInfo = JsonMapper.ToObject<LevelList>(textJson.text); } }
来自:https://blog.csdn.net/virus2014/article/details/52910168