mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -11,6 +11,22 @@ namespace Plugins.JNGame.Util
|
||||
{
|
||||
public Dictionary<string, List<Delegate>> EventHandlers { get; private set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 添加事件监听器
|
||||
/// </summary>
|
||||
/// <param name="eventId">事件标识符</param>
|
||||
/// <param name="listener">事件监听器</param>
|
||||
public void AddListener(string eventId, Delegate listener)
|
||||
{
|
||||
if (!EventHandlers.ContainsKey(eventId))
|
||||
{
|
||||
EventHandlers[eventId] = new List<Delegate>();
|
||||
}
|
||||
|
||||
EventHandlers[eventId].Add(listener);
|
||||
Debug.Log(eventId+ "AddListener" + EventHandlers[eventId].Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加事件监听器
|
||||
/// </summary>
|
||||
@@ -26,7 +42,6 @@ namespace Plugins.JNGame.Util
|
||||
|
||||
EventHandlers[eventId].Add(listener);
|
||||
Debug.Log(eventId+ "AddListener" + EventHandlers[eventId].Count);
|
||||
//eventHandlers[eventId] = (Action<T>)eventHandlers[eventId] + listener;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
62
JNFrame/Assets/Plugins/JNGame/Util/JAPI.cs
Normal file
62
JNFrame/Assets/Plugins/JNGame/Util/JAPI.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace Plugins.JNGame.Util
|
||||
{
|
||||
|
||||
public class JAPIConfig{
|
||||
public string BaseURL = "http://localhost:8080"; //baseURL 默认HTTP头
|
||||
public int Timeout = 5000; //超时时间
|
||||
}
|
||||
|
||||
public class JAPIData<T>{
|
||||
public T Data;
|
||||
}
|
||||
|
||||
/**
|
||||
* JNGame 的 网络请求类
|
||||
*/
|
||||
public class JAPI {
|
||||
|
||||
private JAPIConfig _config;
|
||||
|
||||
public JAPI(JAPIConfig config = null)
|
||||
{
|
||||
if (config == null) config = new JAPIConfig();
|
||||
this._config = config;
|
||||
}
|
||||
|
||||
public async UniTask<T> Get<T>(string url)
|
||||
{
|
||||
var request = UnityWebRequest.Get($"{this._config.BaseURL}/{url}");
|
||||
return JsonConvert.DeserializeObject<T>((await request.SendWebRequest()).downloadHandler.text);
|
||||
}
|
||||
|
||||
public async UniTask<T> Post<T>(string url,Dictionary<string,string> data)
|
||||
{
|
||||
var request = UnityWebRequest.Post($"{this._config.BaseURL}/{url}",data);
|
||||
return JsonConvert.DeserializeObject<T>((await request.SendWebRequest()).downloadHandler.text);
|
||||
}
|
||||
|
||||
public async UniTask<T> Post<T,TA>(string url,TA data)
|
||||
{
|
||||
var request = UnityWebRequest.Post($"{this._config.BaseURL}/{url}",JsonConvert.SerializeObject(data));
|
||||
return JsonConvert.DeserializeObject<T>((await request.SendWebRequest()).downloadHandler.text);
|
||||
}
|
||||
|
||||
public async UniTask<byte[]> GetByte(string url)
|
||||
{
|
||||
var request = UnityWebRequest.Get($"{this._config.BaseURL}/{url}");
|
||||
return (await request.SendWebRequest()).downloadHandler.data;
|
||||
}
|
||||
|
||||
public async UniTask<byte[]> PostByte(string url,Dictionary<string,string> data)
|
||||
{
|
||||
var request = UnityWebRequest.Post($"{this._config.BaseURL}/{url}",data);
|
||||
return (await request.SendWebRequest()).downloadHandler.data;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
JNFrame/Assets/Plugins/JNGame/Util/JAPI.cs.meta
Normal file
3
JNFrame/Assets/Plugins/JNGame/Util/JAPI.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fc72d9190d1241f680950baaaf2ec27e
|
||||
timeCreated: 1706373831
|
Reference in New Issue
Block a user