mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-11-11 08:38:45 +00:00
提交新概念 Tile从服务器
This commit is contained in:
@@ -53,6 +53,15 @@ namespace Plugins.JNGame.Util
|
||||
/// <typeparam name="T">事件参数类型</typeparam>
|
||||
/// <param name="eventId">事件标识符</param>
|
||||
/// <param name="listener">事件监听器</param>
|
||||
public void RemoveListener(string eventId, Action listener)
|
||||
{
|
||||
if (EventHandlers.ContainsKey(eventId))
|
||||
{
|
||||
//eventHandlers[eventId] = (Action<T>)eventHandlers[eventId] - listener;
|
||||
EventHandlers[eventId].Remove(listener);
|
||||
Debug.Log(eventId + "RemoveListener" + EventHandlers[eventId].Count);
|
||||
}
|
||||
}
|
||||
public void RemoveListener<T>(string eventId, Action<T> listener)
|
||||
{
|
||||
if (EventHandlers.ContainsKey(eventId))
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Sockets;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
|
||||
namespace Plugins.JNGame.Util
|
||||
{
|
||||
@@ -15,5 +18,25 @@ namespace Plugins.JNGame.Util
|
||||
BitConverter.GetBytes(value).CopyTo(result, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] ObjectToBytes<T>(T data)
|
||||
{
|
||||
var formatter = new BinaryFormatter();
|
||||
using var mStream = new MemoryStream();
|
||||
formatter.Serialize(mStream, data);
|
||||
mStream.Flush();
|
||||
return mStream.GetBuffer();
|
||||
}
|
||||
|
||||
public static T BytesToObject<T>(byte[] data)
|
||||
{
|
||||
var formatter = new BinaryFormatter();
|
||||
using var mStream = new MemoryStream();
|
||||
mStream.Write(data, 0, data.Length);
|
||||
mStream.Flush();
|
||||
mStream.Seek(0, SeekOrigin.Begin);
|
||||
return (T)formatter.Deserialize(mStream);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
78
JNFrame2/Assets/JNGame/Util/UnityMainThreadDispatcher.cs
Normal file
78
JNFrame2/Assets/JNGame/Util/UnityMainThreadDispatcher.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JNGame.Util
|
||||
{
|
||||
/// Author: Pim de Witte (pimdewitte.com) and contributors, https://github.com/PimDeWitte/UnityMainThreadDispatcher
|
||||
/// <summary>
|
||||
/// A thread-safe class which holds a queue with actions to execute on the next Update() method. It can be used to make calls to the main thread for
|
||||
/// things such as UI Manipulation in Unity. It was developed for use in combination with the Firebase Unity plugin, which uses separate threads for event handling
|
||||
/// </summary>
|
||||
public class UnityMainThreadDispatcher : SingletonScene<UnityMainThreadDispatcher> {
|
||||
|
||||
private readonly Queue<Action> _executionQueue = new Queue<Action>();
|
||||
|
||||
public void Update() {
|
||||
lock(_executionQueue) {
|
||||
while (_executionQueue.Count > 0) {
|
||||
_executionQueue.Dequeue().Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locks the queue and adds the IEnumerator to the queue
|
||||
/// </summary>
|
||||
/// <param name="action">IEnumerator function that will be executed from the main thread.</param>
|
||||
public void Enqueue(IEnumerator action) {
|
||||
lock (_executionQueue) {
|
||||
_executionQueue.Enqueue (() => {
|
||||
StartCoroutine(action);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locks the queue and adds the Action to the queue
|
||||
/// </summary>
|
||||
/// <param name="action">function that will be executed from the main thread.</param>
|
||||
public void Enqueue(Action action)
|
||||
{
|
||||
Enqueue(ActionWrapper(action));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Locks the queue and adds the Action to the queue, returning a Task which is completed when the action completes
|
||||
/// </summary>
|
||||
/// <param name="action">function that will be executed from the main thread.</param>
|
||||
/// <returns>A Task that can be awaited until the action completes</returns>
|
||||
public Task EnqueueAsync(Action action)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<bool>();
|
||||
|
||||
void WrappedAction() {
|
||||
try
|
||||
{
|
||||
action();
|
||||
tcs.TrySetResult(true);
|
||||
} catch (Exception ex)
|
||||
{
|
||||
tcs.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
Enqueue(ActionWrapper(WrappedAction));
|
||||
return tcs.Task;
|
||||
}
|
||||
|
||||
|
||||
IEnumerator ActionWrapper(Action a)
|
||||
{
|
||||
a();
|
||||
yield return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d1e1ab4d41f4a618dcf3ba40014cf20
|
||||
timeCreated: 1724926741
|
||||
Reference in New Issue
Block a user