本文主要是介绍U3d-Json读取数组,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
U3d-Json读取数组
Json文件:
{"level1":["[1,1,1,1,1,1,1,1,1,1]","[1,1,1,1,1,1,0,4,1,1]","[1,1,1,1,1,1,0,0,1,1]","[1,1,1,1,1,1,3,0,1,1]","[1,1,1,1,1,1,0,0,1,1]","[1,1,1,1,1,1,0,0,1,1]","[1,1,1,1,1,1,0,0,1,1]","[1,1,1,1,1,1,1,1,1,1]"],"level2":["[1,1,1,1,1,1,1,1,1,1]","[1,1,1,3,0,1,2,1,1,1]","[1,1,1,0,0,1,0,1,1,1]","[1,1,1,0,0,0,0,0,4,1]","[1,1,1,0,0,0,0,0,0,1]","[1,1,1,1,0,1,0,0,1,1]","[1,1,1,1,0,1,0,0,1,1]","[1,1,1,1,1,1,1,1,1,1]"],"level3":["[1,1,1,1,1,1,1,1,1,1]","[1,1,1,0,0,0,0,1,1,1]","[1,1,0,0,0,0,0,0,0,1]","[1,1,0,1,1,1,0,0,0,1]","[1,0,0,0,1,1,0,0,0,1]","[1,4,0,0,0,1,2,0,3,1]","[1,1,0,0,0,1,0,0,0,1]","[1,1,1,1,1,1,1,1,1,1]"],"level4":["[1,1,1,4,0,0,0,0,0,1]","[1,1,1,1,1,1,1,0,0,1]","[1,1,0,0,0,0,1,0,0,1]","[1,0,0,0,0,0,1,0,0,1]","[1,0,0,1,1,0,0,3,0,1]","[1,0,0,0,0,0,2,0,2,1]","[1,0,0,0,0,0,0,0,0,1]","[1,0,0,1,0,0,1,1,1,1]"],"level5":["[1,1,1,1,0,0,1,0,1,1]","[1,1,1,4,0,0,0,0,1,1]","[1,1,1,2,0,0,0,1,1,1]","[1,1,1,0,1,1,0,2,1,1]","[1,1,3,0,0,1,0,0,1,1]","[1,1,0,0,0,0,0,0,1,1]","[1,1,1,0,0,0,0,0,1,1]","[1,1,1,0,1,1,0,0,1,1]"],"level6":["[1,1,1,1,1,1,1,1,1,1]","[1,1,1,1,0,0,0,0,4,1]","[1,1,1,1,1,1,0,0,0,0]","[1,1,1,1,0,0,0,0,0,3]","[1,1,1,1,0,0,0,0,0,0]","[1,1,1,1,0,2,0,0,0,0]","[1,1,1,1,1,1,0,0,0,0]","[1,1,1,1,1,1,0,0,1,1]"],"level7":["[1,1,1,1,1,0,0,0,0,4]","[1,1,0,0,0,0,0,0,0,2]","[1,1,0,0,0,0,1,1,0,0]","[1,1,0,0,0,0,0,1,0,0]","[1,1,0,0,0,0,0,0,2,0]","[1,1,0,0,0,1,0,0,2,0]","[1,1,0,0,3,0,0,0,2,0]","[1,1,1,1,1,1,1,0,0,0]"],"level8":["[0,0,0,0,0,0,0,1,1,1]","[0,1,1,1,0,0,0,4,0,0]","[0,1,0,0,0,0,0,0,1,0]","[0,2,0,0,0,0,0,0,1,0]","[2,2,0,1,0,0,0,0,1,0]","[1,1,0,0,0,0,0,0,0,0]","[1,3,0,0,0,0,0,0,0,1]","[1,0,0,1,1,0,0,0,1,1]"],"level9":["[4,2,0,0,0,0,0,0,0,0]","[1,1,1,1,1,1,1,1,1,0]","[0,0,0,0,0,0,0,0,0,2]","[0,1,1,1,1,1,1,1,1,1]","[2,0,0,0,0,0,0,0,0,0]","[1,1,1,1,1,1,1,1,1,0]","[3,0,0,0,0,0,0,0,0,2]","[1,1,1,1,1,1,1,1,1,1]"],"level10":["[1,1,0,0,0,0,0,0,0,4]",
"[1,1,0,1,1,0,0,0,1,2]",
"[1,1,0,2,0,0,1,1,1,0]",
"[1,1,0,0,0,0,1,0,0,3]",
"[1,1,0,1,0,0,0,0,0,0]",
"[1,1,0,1,0,0,0,1,0,0]",
"[1,1,0,1,0,1,0,1,1,1]",
"[1,1,0,1,0,0,0,0,0,2]"]
}
//读取
using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;[Serializable]
public class LevelInfo
{public int[,] Levellist = new int[,] { };public int Wight { get; set; } //宽public int Height { get; set; } //高
}
public class LevelDate
{public static List<LevelInfo> GetLevel(){List<LevelInfo> LevelList = new List<LevelInfo>();TextAsset asset = Resources.Load<TextAsset>("Content/Levels");//Debug.Log(asset.text);JsonData data = JsonMapper.ToObject(asset.text);//Debug.Log(data.Count);for (int i = 0; i < data.Count; i++)//data.Count = 2{//JsonData data_1 = data[i];//Debug.Log(data[i].Count);LevelInfo levelinfo = new LevelInfo();List<string[]> Linshi = new List<string[]>();for (int j = 0; j < data[i].Count; j++) //data[i].Count = 8{//把读出来的第一列字符串的数字分割出来保存在Character_array数组里string[] Character_array = (data[i][j].ToString()).Split(new char[3] {'[',',',']'});Linshi.Add(Character_array);}//Debug.Log(Linshi.Count + "-----------");levelinfo.Height = Linshi.Count; //高levelinfo.Wight = Linshi[0].Length; //宽levelinfo.Levellist = new int[levelinfo.Height, levelinfo.Wight];for (int n = 0; n < Linshi.Count; n++) //levelinfo.Linshi.Count = 8{//为什么k = 1,k<levelinfo.Linshi[n].Length-1,因为前后有空格for (int k = 1; k < Linshi[n].Length-1; k++) // levelinfo.Linshi[n] = 10{//Debug.Log(Int32.Parse(Linshi[n][k]));levelinfo.Levellist[n, k - 1] = Int32.Parse(Linshi[n][k]);} }LevelList.Add(levelinfo);}return LevelList;}
}
这篇关于U3d-Json读取数组的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!