chenxin
2020-12-18 7b9712f33f42b14e4d277166fa533a379021312d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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();
 
 
    }
}