This commit is contained in:
PC-20230316NUNE\Administrator
2024-02-01 19:06:51 +08:00
parent aa4d6c3ce2
commit 877dca3b43
7518 changed files with 653768 additions and 162059 deletions

View File

@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Plugins.JNGame.Util
{
/// <summary>
/// 静态事件分发器
/// </summary>
public class EventDispatcher
{
public static readonly EventDispatcher Event = new EventDispatcher();
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, Action listener)
{
if (!EventHandlers.ContainsKey(eventId))
{
EventHandlers[eventId] = new List<Delegate>();
}
EventHandlers[eventId].Add(listener);
Debug.Log(eventId+ "AddListener" + EventHandlers[eventId].Count);
}
/// <summary>
/// 添加事件监听器
/// </summary>
/// <typeparam name="T">事件参数类型</typeparam>
/// <param name="eventId">事件标识符</param>
/// <param name="listener">事件监听器</param>
public void AddListener<T>(string eventId, Action<T> listener)
{
if (!EventHandlers.ContainsKey(eventId))
{
EventHandlers[eventId] = new List<Delegate>();
}
EventHandlers[eventId].Add(listener);
Debug.Log(eventId+ "AddListener" + EventHandlers[eventId].Count);
}
/// <summary>
/// 移除事件监听器
/// </summary>
/// <typeparam name="T">事件参数类型</typeparam>
/// <param name="eventId">事件标识符</param>
/// <param name="listener">事件监听器</param>
public void RemoveListener<T>(string eventId, Action<T> listener)
{
if (EventHandlers.ContainsKey(eventId))
{
//eventHandlers[eventId] = (Action<T>)eventHandlers[eventId] - listener;
EventHandlers[eventId].Remove(listener);
Debug.Log(eventId + "RemoveListener" + EventHandlers[eventId].Count);
}
}
/// <summary>
/// 分发事件
/// </summary>
/// <typeparam name="T">事件参数类型</typeparam>
/// <param name="eventId">事件标识符</param>
/// <param name="args">事件参数</param>
public void Dispatch<T>(string eventId, T args)
{
if (EventHandlers.ContainsKey(eventId) && EventHandlers[eventId] != null)
{
foreach(Delegate fun in EventHandlers[eventId])
{
// 确保 fun 实际上是指向一个 Action<T> 类型的函数
if (fun.Method.GetParameters().Length == 1 && fun.Method.GetParameters()[0].ParameterType == typeof(T))
{
((Action<T>)fun)(args);
}
}
}
}
public void Dispatch(string eventId)
{
if (EventHandlers.ContainsKey(eventId) && EventHandlers[eventId] != null)
{
foreach(Delegate fun in EventHandlers[eventId])
{
// 确保 fun 实际上是指向一个 Action<T> 类型的函数
if (fun.Method.GetParameters().Length == 0)
{
((Action)fun)();
}
}
}
}
public void Reset()
{
EventHandlers = new();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 311db7dc834c409b81610b20f5510988
timeCreated: 1706065209

View 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.PostWwwForm($"{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;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fc72d9190d1241f680950baaaf2ec27e
timeCreated: 1706373831

View File

@@ -0,0 +1,11 @@
namespace Plugins.JNGame.Util
{
//Proto工具
public class ProtoUtil
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 37c91935ad854ee1a4b199391df35004
timeCreated: 1706006228

View File

@@ -0,0 +1,62 @@
using System;
using UnityEngine;
namespace Plugins.JNGame.Util
{
public static class RandomUtil
{
public static Func<float> SyncRandom(int seed = 1000000,int start = 10)
{
// 线性同余生成器的参数
const int a = 1103515245;
const int m = 2147483647; // 2^31 - 1
const int increment = 16807;
for (var i = 0; i < start; i++)
{
seed = (a * seed + increment) % m;
}
return () =>
{
seed = (a * seed + increment) % m;
var rnd = Math.Abs((float)seed / m); // 转换为0.0到1.0之间的数
return rnd; // 返回随机浮点数
};
}
public static Func<float,float,float> SyncRandomFloat(int seed = 1000000,int start = 10)
{
var nRandom = SyncRandom(seed, start);
return (float min,float max) =>
{
var random = nRandom();
return random * (max - min) + min;
};
}
public static Func<int,int,int> SyncRandomInt(int seed = 2000000,int start = 10)
{
var nRandom = SyncRandom(seed, start);
return (min, max) =>
{
var random = nRandom();
return (int)Math.Round(random * (max - min) + min);
};
}
public static Func<int> Next(int start)
{
return () => start++;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 04c0ca6d0b7f4dfab57c8dca2a3a7b94
timeCreated: 1706164470

View File

@@ -0,0 +1,32 @@
namespace Plugins.JNGame.Util
{
public abstract class SingletonUtil<T> where T : Singleton<T>,new() {
public static T Instance{
get{
if (Singleton<T>.ins == null)
{
Singleton<T>.ins = new T();
Singleton<T>.ins.Init();
}
return Singleton<T>.ins;
}
}
public static void Clean()
{
Singleton<T>.ins.Clean();
Singleton<T>.ins = null;
}
}
public class Singleton<T> {
public static T ins;
public virtual void Init(){}
public virtual void Clean(){}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 80ef0477d1c9478a8d73b1e6ac21a8b9
timeCreated: 1706167436

View File

@@ -0,0 +1,85 @@
using UnityEngine;
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
///
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
public class SingletonScene<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance;
private static object _lock = new object();
public static T Instance
{
get
{
if (applicationIsQuitting)
{
Debug.LogWarning("[Singleton] Instance '" + typeof(T) +
"' already destroyed on application quit." +
" Won't create again - returning null.");
return null;
}
lock (_lock)
{
if (_instance == null)
{
_instance = (T)FindObjectOfType(typeof(T));
if (FindObjectsOfType(typeof(T)).Length > 1)
{
Debug.LogError("[Singleton] Something went really wrong " +
" - there should never be more than 1 singleton!" +
" Reopening the scene might fix it.");
return _instance;
}
if (_instance == null)
{
GameObject singleton = new GameObject();
_instance = singleton.AddComponent<T>();
singleton.name = "(singleton) " + typeof(T).ToString();
DontDestroyOnLoad(singleton);
Debug.Log("[Singleton] An instance of " + typeof(T) +
" is needed in the scene, so '" + singleton +
"' was created with DontDestroyOnLoad.");
}
else
{
Debug.Log("[Singleton] Using instance already created: " +
_instance.gameObject.name);
}
}
return _instance;
}
}
}
private static bool applicationIsQuitting = false;
/// <summary>
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
public void OnDestroy()
{
applicationIsQuitting = true;
OnDispose();
}
protected virtual void OnDispose()
{
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c9277ee16036443695be9ff1ff09df12
timeCreated: 1706168129

View File

@@ -0,0 +1,19 @@
using System;
namespace Plugins.JNGame.Util
{
public class ToUtil
{
public static int Byte4ToInt(byte[] data)
{
return BitConverter.ToInt32(data, 0);
}
public static byte[] IntToByte4(int value)
{
byte[] result = new byte[4];
BitConverter.GetBytes(value).CopyTo(result, 0);
return result;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 22d4030ad259466884c3fefc632770f9
timeCreated: 1706004960