增加JsonUtil工具类

This commit is contained in:
monitor1394
2024-05-09 13:13:51 +08:00
parent b86cae6717
commit 4e3182edfc
10 changed files with 161 additions and 24 deletions

View File

@@ -0,0 +1,73 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Networking;
namespace XCharts.Runtime
{
public static class JsonUtil
{
public static IEnumerator GetWebJson<T>(string url, Action<T[]> callback)
{
var www = UnityWebRequest.Get(url);
yield return www;
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("GetWebJson Error: " + www.error);
}
else
{
var json = www.downloadHandler.text.Trim();
callback(GetJsonArray<T>(json));
www.Dispose();
}
}
public static IEnumerator GetWebJson<T>(string url, Action<T> callback)
{
var www = UnityWebRequest.Get(url);
yield return www;
#if UNITY_2020_1_OR_NEWER
if (www.result != UnityWebRequest.Result.Success)
#else
if (www.isNetworkError || www.isHttpError)
#endif
{
Debug.LogError("GetWebJson Error: " + www.error);
}
else
{
var json = www.downloadHandler.text.Trim();
callback(GetJsonObject<T>(json));
www.Dispose();
}
}
public static T GetJsonObject<T>(string json)
{
return JsonUtility.FromJson<T>(json);
}
public static T[] GetJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[Serializable]
private class Wrapper<T>
{
#pragma warning disable 0649
public T[] array;
#pragma warning restore 0649
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 88e9115d32af34a3dae0d5c3e32de41c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: