提交帧同步案例

This commit is contained in:
PC-20230316NUNE\Administrator
2024-01-26 19:15:07 +08:00
parent 3a345ab966
commit 68c4d5e811
3928 changed files with 463020 additions and 1 deletions

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Plugins.JNGame.Util
{
/// <summary>
/// 静态事件分发器
/// </summary>
public class EventDispatcher
{
public Dictionary<string, List<Delegate>> EventHandlers { get; private set; } = new();
/// <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);
//eventHandlers[eventId] = (Action<T>)eventHandlers[eventId] + listener;
}
/// <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 Reset()
{
EventHandlers = new();
}
}
}

View File

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

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,38 @@
using System;
namespace Plugins.JNGame.Util
{
public static class RandomUtil
{
public static Func<double> SyncRandom(int seed,int start = 1)
{
for(var i = 0; i < start; i++){
seed = (seed * 9301 + 49297) % 233280;
}
return () =>
{
seed = (seed * 9301 + 49297) % 233280;
var rnd = seed / 233280.0;
return 0 + rnd * (1 - 0);
};
}
public static Func<int,int,int> SyncRandomInt(int seed,int start = 1)
{
var nRandom = SyncRandom(seed, start);
return (min, max) => (int)(Math.Round(nRandom() * (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