BITKit/Packages/Runtime~/Unity/Common/Scripts/Network/NetworkPost.cs

84 lines
2.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using System.Threading;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net;
using System.Net.Http;
using UnityEngine.UIElements;
using System.Text;
using System.Linq;
using BITKit.HttpNet;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace BITKit
{
public class NetworkPost : Provider
{
[SubclassSelector, SerializeReference] public References _url;
[SubclassSelector, SerializeReference] public WebProvider webProvider;
public string result;
public int postCount;
public int resultCount;
public string content;
private readonly LimitTimes limitTimes = new();
public async void Set(object value)
{
postCount++;
content = await webProvider.PostAsync(_url, value);
resultCount++;
}
public override async void Set<T>(T obj)
{
switch (obj)
{
case WWWForm formFields:
if (limitTimes)
{
try
{
NetworkCore.Add(_url);
postCount++;
result = await webProvider.PostAsync(_url, formFields);
resultCount++;
limitTimes.Release();
content = JsonConvert.SerializeObject(formFields);
}
catch (System.Exception e)
{
if (e is not OperationCanceledException or TimeoutException)
{
Debug.LogException(e, this);
}
limitTimes.Release();
}
}
break;
case string _string:
break;
}
}
}
#if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(NetworkPost))]
public class NetworkPostInspector : BITInspector<NetworkPost>
{
protected override void OnAwake()
{
var uss = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/BITKit/UX/Common/NetworkPost");
root.root.styleSheets.Add(uss);
}
}
#endif
}