This commit is contained in:
2025-09-17 18:56:28 +08:00
commit 54c72710a5
5244 changed files with 5717609 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
# UniFramework.Event
一个轻量级的事件系统。

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 5dfad42c0d1a02f429a99108a35bbfce
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e86e06e6a95e6cc43940623bcc15721c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace UniFramework.Event
{
public class EventGroup
{
private readonly Dictionary<System.Type, List<Action<IEventMessage>>> _cachedListener = new Dictionary<System.Type, List<Action<IEventMessage>>>();
/// <summary>
/// 添加一个监听
/// </summary>
public void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
{
System.Type eventType = typeof(TEvent);
if (_cachedListener.ContainsKey(eventType) == false)
_cachedListener.Add(eventType, new List<Action<IEventMessage>>());
if (_cachedListener[eventType].Contains(listener) == false)
{
_cachedListener[eventType].Add(listener);
UniEvent.AddListener(eventType, listener);
}
else
{
UniLogger.Warning($"Event listener is exist : {eventType}");
}
}
/// <summary>
/// 移除所有缓存的监听
/// </summary>
public void RemoveAllListener()
{
foreach (var pair in _cachedListener)
{
System.Type eventType = pair.Key;
for (int i = 0; i < pair.Value.Count; i++)
{
UniEvent.RemoveListener(eventType, pair.Value[i]);
}
pair.Value.Clear();
}
_cachedListener.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dcc5e4008d087434eb5db1390d8e4e6a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,7 @@

namespace UniFramework.Event
{
public interface IEventMessage
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7dcd12aabf986c6468f748911b1af65a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,206 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UniFramework.Event
{
public static class UniEvent
{
private class PostWrapper
{
public int PostFrame;
public int EventID;
public IEventMessage Message;
public void OnRelease()
{
PostFrame = 0;
EventID = 0;
Message = null;
}
}
private static bool _isInitialize = false;
private static GameObject _driver = null;
private static readonly Dictionary<int, LinkedList<Action<IEventMessage>>> _listeners = new Dictionary<int, LinkedList<Action<IEventMessage>>>(1000);
private static readonly List<PostWrapper> _postingList = new List<PostWrapper>(1000);
/// <summary>
/// 初始化事件系统
/// </summary>
public static void Initalize()
{
if (_isInitialize)
throw new Exception($"{nameof(UniEvent)} is initialized !");
if (_isInitialize == false)
{
// 创建驱动器
_isInitialize = true;
_driver = new UnityEngine.GameObject($"[{nameof(UniEvent)}]");
_driver.AddComponent<UniEventDriver>();
UnityEngine.Object.DontDestroyOnLoad(_driver);
UniLogger.Log($"{nameof(UniEvent)} initalize !");
}
}
/// <summary>
/// 销毁事件系统
/// </summary>
public static void Destroy()
{
if (_isInitialize)
{
ClearAll();
_isInitialize = false;
if (_driver != null)
GameObject.Destroy(_driver);
UniLogger.Log($"{nameof(UniEvent)} destroy all !");
}
}
/// <summary>
/// 更新事件系统
/// </summary>
internal static void Update()
{
for (int i = _postingList.Count - 1; i >= 0; i--)
{
var wrapper = _postingList[i];
if (UnityEngine.Time.frameCount > wrapper.PostFrame)
{
SendMessage(wrapper.EventID, wrapper.Message);
_postingList.RemoveAt(i);
}
}
}
/// <summary>
/// 清空所有监听
/// </summary>
public static void ClearAll()
{
foreach (int eventId in _listeners.Keys)
{
_listeners[eventId].Clear();
}
_listeners.Clear();
_postingList.Clear();
}
/// <summary>
/// 添加监听
/// </summary>
public static void AddListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
{
System.Type eventType = typeof(TEvent);
int eventId = eventType.GetHashCode();
AddListener(eventId, listener);
}
/// <summary>
/// 添加监听
/// </summary>
public static void AddListener(System.Type eventType, System.Action<IEventMessage> listener)
{
int eventId = eventType.GetHashCode();
AddListener(eventId, listener);
}
/// <summary>
/// 添加监听
/// </summary>
public static void AddListener(int eventId, System.Action<IEventMessage> listener)
{
if (_listeners.ContainsKey(eventId) == false)
_listeners.Add(eventId, new LinkedList<Action<IEventMessage>>());
if (_listeners[eventId].Contains(listener) == false)
_listeners[eventId].AddLast(listener);
}
/// <summary>
/// 移除监听
/// </summary>
public static void RemoveListener<TEvent>(System.Action<IEventMessage> listener) where TEvent : IEventMessage
{
System.Type eventType = typeof(TEvent);
int eventId = eventType.GetHashCode();
RemoveListener(eventId, listener);
}
/// <summary>
/// 移除监听
/// </summary>
public static void RemoveListener(System.Type eventType, System.Action<IEventMessage> listener)
{
int eventId = eventType.GetHashCode();
RemoveListener(eventId, listener);
}
/// <summary>
/// 移除监听
/// </summary>
public static void RemoveListener(int eventId, System.Action<IEventMessage> listener)
{
if (_listeners.ContainsKey(eventId))
{
if (_listeners[eventId].Contains(listener))
_listeners[eventId].Remove(listener);
}
}
/// <summary>
/// 实时广播事件
/// </summary>
public static void SendMessage(IEventMessage message)
{
int eventId = message.GetType().GetHashCode();
SendMessage(eventId, message);
}
/// <summary>
/// 实时广播事件
/// </summary>
public static void SendMessage(int eventId, IEventMessage message)
{
if (_listeners.ContainsKey(eventId) == false)
return;
LinkedList<Action<IEventMessage>> listeners = _listeners[eventId];
if (listeners.Count > 0)
{
var currentNode = listeners.Last;
while (currentNode != null)
{
currentNode.Value.Invoke(message);
currentNode = currentNode.Previous;
}
}
}
/// <summary>
/// 延迟广播事件
/// </summary>
public static void PostMessage(IEventMessage message)
{
int eventId = message.GetType().GetHashCode();
PostMessage(eventId, message);
}
/// <summary>
/// 延迟广播事件
/// </summary>
public static void PostMessage(int eventId, IEventMessage message)
{
var wrapper = new PostWrapper();
wrapper.PostFrame = UnityEngine.Time.frameCount;
wrapper.EventID = eventId;
wrapper.Message = message;
_postingList.Add(wrapper);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a2a39e0393e22c44b8416d1445c9bb2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
using UnityEngine;
namespace UniFramework.Event
{
internal class UniEventDriver : MonoBehaviour
{
void Update()
{
UniEvent.Update();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 91e57d06457f3ec40914e4d3ed58c070
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
{
"name": "UniFramework.Event",
"rootNamespace": "",
"references": [
"GUID:e34a5702dd353724aa315fb8011f08c3"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: f22fac247a56d2d41b687bb0d900e54e
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
using System.Diagnostics;
namespace UniFramework.Event
{
internal static class UniLogger
{
[Conditional("DEBUG")]
public static void Log(string info)
{
UnityEngine.Debug.Log(info);
}
public static void Warning(string info)
{
UnityEngine.Debug.LogWarning(info);
}
public static void Error(string info)
{
UnityEngine.Debug.LogError(info);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9bb2f71f321705a4286de583f1c930d7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: