liuzhiwei
2020-12-12 6567bd7483c6aeb490869b5df3e7746bde01425a
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
using LitJson;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using System.Collections.Generic;
using System.Collections;
 
public class JsonController
{
    private static JsonController _Instance;
    public static JsonController Instance
    {
        get
        {
            if (_Instance == null)
            {
                _Instance = new JsonController();
            }
            return _Instance;
        }
    }
 
    /// <summary>
    /// 初始化增加对于V2V3的支持
    /// </summary>
    public JsonController()
    {
        Action<Vector2, JsonWriter> writeVector2 = (v, w) =>
        {
            w.WriteObjectStart();
            w.WritePropertyName("x");
            w.Write(v.x.ToString());
 
            w.WritePropertyName("y");
            w.Write(v.y.ToString());
 
            w.WriteObjectEnd();
        };
 
        JsonMapper.RegisterExporter<Vector2>((v, w) =>
        {
            writeVector2(v, w);
        });
 
        Action<Vector3, JsonWriter> writeVector3 = (v, w) =>
        {
            w.WriteObjectStart();
            w.WritePropertyName("x");
            w.Write(v.x.ToString());
            w.WritePropertyName("y");
            w.Write(v.y.ToString());
            w.WritePropertyName("z");
            w.Write(v.z.ToString());
            w.WriteObjectEnd();
        };
 
        JsonMapper.RegisterExporter<Vector3>((v, w) =>
        {
            writeVector3(v, w);
        });
    }
 
    public void Init() { }
    /// <summary>
    /// /// 保存Json成文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="o"></param>
    public void SaveJson(string path, object o)
    {
        string directoryName = Path.GetDirectoryName(path);
        if (!Directory.Exists(directoryName))
        {
            Directory.CreateDirectory(directoryName);
        }
        JsonData data = JsonMapper.ToJson(o);
        File.WriteAllText(path, data.ToString());
    }
 
    /// <summary>
    /// 从文件中读取Json
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public JsonData ReadFromFile(string path)
    {
        string data = File.ReadAllText(path);
 
        return JsonMapper.ToObject(data.ToString());
    }
 
 
    public IEnumerator WWWReadFromFile(string root, string[] paths, Action<Dictionary<string, JsonData>> ac)
    {
        Dictionary<string, JsonData> allDatas = new Dictionary<string, JsonData>();
        for (int i = 0; i < paths.Length; i++)
        {
            string tablename = paths[i];
            string filepath = root + tablename;
            UnityWebRequest www = UnityWebRequest.Get(filepath);
            yield return www.SendWebRequest();
            if (www.isHttpError || www.isNetworkError)
            {
                Debug.LogError("UnityWebRequest加载出错");
                yield break;
            }
            string data = www.downloadHandler.text;
            JsonData tmpJsonData = JsonMapper.ToObject(data.ToString());
            allDatas.Add(tablename, tmpJsonData);
        }
 
        if (ac != null)
        {
            ac(allDatas);
        }
    }
}