BITKit/Packages/Runtime~/Unity/Scripts/Data/ApplyJson.cs

93 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Net;
using UnityEngine.UIElements;
using System.IO;
namespace BITKit
{
public class ApplyJson : Provider
{
public TranslateSO translateSO;
public override void Set<T>(T obj)
{
switch (obj)
{
case string _string:
if (translateSO)
{
_string = translateSO.Get(_string, false);
}
DataParser.Set(_string);
Debug.Log("已加载Json配置...");
break;
case IDictionary<string, string> _dictionary:
if (_dictionary.Count is 1 && _dictionary.Keys.ElementAt(0) is "value" or "json")
{
DataParser.Set(_dictionary.Values.ElementAt(0));
}
foreach (var pair in _dictionary)
{
DataParser.Set(pair.Key, pair.Value);
}
break;
case HttpListenerRequest _request:
System.IO.Stream body = _request.InputStream;
System.Text.Encoding encoding = _request.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
if (_request.ContentType != null)
{
// Debug.Log("Client data content type {0}" + _request.ContentType);
}
//Debug.Log("Client data content length {0}" + _request.ContentLength64);
//Debug.Log("Start of client data:");
// Convert the data to a string and display it on the console.
string s = reader.ReadToEnd();
//Debug.Log(s);
//Debug.Log("End of client data:");
body.Close();
reader.Close();
if (translateSO)
{
s = translateSO.Get(s, false);
}
DataParser.Set(s);
break;
}
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(ApplyJson))]
public class ApplyJsonInspector : BITInspector<ApplyJson>
{
TextField textField;
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector(root, serializedObject, true);
textField = root.Create<TextField>();
var button = root.Create<Button>();
textField.isDelayed = true;
textField.multiline = true;
button.clicked += Excute;
button.text = "应用Json";
return root;
}
void Excute()
{
BITAppForUnity.ThrowIfNotPlaying();
agent.Set(textField.value);
}
}
#endif
}