using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using LitJson;
|
using KTGMGemClient;
|
using UnityEngine.UI;
|
public class JsonDemo : MonoBehaviour
|
{
|
public Text text;
|
// Start is called before the first frame update
|
void Start()
|
{
|
// List<Solder> allSolder = new List<Solder>();
|
// float[] myBullet = new float[] { 1, 2 };
|
// Solder a = new Solder(myBullet, new Vector2(1, 1));
|
// myBullet = new float[] { 3, 4 };
|
// Solder b = new Solder(myBullet, new Vector2(2, 2));
|
// allSolder.Add(a);
|
// allSolder.Add(b);
|
|
// Hero hero = new Hero(10,170.5f,"大宝剑",0, new Vector3(1, 0, 1), allSolder);
|
// string path = Application.streamingAssetsPath + "/TestJson/TestJson.txt";
|
// JsonController.Instance.SaveJson(path, hero);
|
|
// StartCoroutine(Read(path));
|
|
//string path = Application.dataPath+"/Resources/Table/";
|
text.text = "Loading";
|
JsonDataReader.Instance.StartRead(AfterRead);
|
|
}
|
|
void AfterRead()
|
{
|
Debug.Log("完成");
|
text.text = "完成 ,路径:" + Application.streamingAssetsPath;
|
}
|
|
IEnumerator Read(string path)
|
{
|
yield return new WaitForSeconds(2);
|
|
JsonData data = JsonController.Instance.ReadFromFile(path);
|
Hero heroB = new Hero();
|
|
heroB.Deserialization(data);
|
Debug.Log(heroB.startPos);
|
}
|
|
}
|
|
public class Hero
|
{
|
public int age;
|
public float height;
|
public string name;
|
public byte gender;
|
public Vector3 startPos;
|
public List<Solder> mySolder;
|
public Hero()
|
{
|
|
}
|
//Vector3不支持,需要自己写
|
public Hero(int age, float height, string name, byte gender, Vector3 startPos, List<Solder> mySolder)
|
{
|
this.age = age;
|
this.height = height;
|
this.name = name;
|
this.gender = gender;
|
this.mySolder = mySolder;
|
this.startPos = startPos;
|
}
|
|
public void Deserialization(JsonData data)
|
{
|
age = int.Parse(data["age"].ToString());
|
height = float.Parse(data["height"].ToString());
|
name = data["name"].ToString();
|
gender = byte.Parse(data["gender"].ToString());
|
|
startPos = new Vector3();
|
startPos.x = float.Parse(data["startPos"]["x"].ToString());
|
startPos.y = float.Parse(data["startPos"]["y"].ToString());
|
startPos.z = float.Parse(data["startPos"]["z"].ToString());
|
|
mySolder = new List<Solder>();
|
JsonData solders = data["mySolder"];
|
foreach (JsonData item in solders)
|
{
|
Solder tmp = new Solder();
|
tmp.Deserialization(item);
|
mySolder.Add(tmp);
|
}
|
}
|
}
|
|
public class Solder
|
{
|
public float[] myBullet;
|
public Vector2 testV2;
|
public Solder()
|
{
|
|
}
|
public Solder(float[] myBullet, Vector2 testV2)
|
{
|
this.myBullet = myBullet;
|
this.testV2 = testV2;
|
}
|
|
//只负责解析Vector3 和Vector2
|
public void Deserialization(JsonData data)
|
{
|
testV2 = new Vector2();
|
testV2.x = float.Parse(data["testV2"]["x"].ToString());
|
testV2.y = float.Parse(data["testV2"]["y"].ToString());
|
|
List<float> allF = new List<float>();
|
foreach (JsonData item in data["myBullet"])
|
{
|
allF.Add(float.Parse(item.ToString()));
|
}
|
myBullet = allF.ToArray();
|
|
|
}
|
}
|