添加界面事件处理

This commit is contained in:
梦语 2024-11-01 14:07:47 +08:00
parent 37705271c5
commit 07b26e6c33
8 changed files with 556 additions and 222 deletions

File diff suppressed because one or more lines are too long

View File

@ -13,7 +13,7 @@ using UnityEngine;
namespace Ether namespace Ether
{ {
public class Boot : SingletonForMono<Boot> public class Boot : Entity
{ {
private void Start() private void Start()
{ {
@ -23,7 +23,6 @@ namespace Ether
EtherInputManager.Inst.Init(); EtherInputManager.Inst.Init();
UIManager.Inst.Init(); UIManager.Inst.Init();
DontDestroyOnLoad(gameObject);
Application.runInBackground = true; Application.runInBackground = true;
OnInit(); OnInit();

View File

@ -28,33 +28,33 @@ public class EventCenter
{ {
#region #region
private static Dictionary<string, Delegate> normalEventDic = new Dictionary<string, Delegate>(); private static Dictionary<string, Delegate> allEventDic = new Dictionary<string, Delegate>();
private static void OnListenerAdding(string eventType, Delegate callBack) private static void OnListenerAdding(string eventType, Delegate callback)
{ {
normalEventDic.TryAdd(eventType, null); allEventDic.TryAdd(eventType, null);
Delegate d = normalEventDic[eventType]; Delegate d = allEventDic[eventType];
if (!string.IsNullOrEmpty(eventType) && d != null && d.GetType() != callBack.GetType()) if (!string.IsNullOrEmpty(eventType) && d != null && d.GetType() != callback.GetType())
{ {
throw new Exception($"尝试为事件{eventType}添加不同类型的委托,当前事件所对应的委托为{d.GetType()},要添加的委托类型为{callBack.GetType()}"); throw new Exception($"尝试为事件{eventType}添加不同类型的委托,当前事件所对应的委托为{d.GetType()},要添加的委托类型为{callback.GetType()}");
} }
} }
private static void OnListenerRemoving(string eventType, Delegate callBack) private static void OnListenerRemoving(string eventType, Delegate callback)
{ {
if (normalEventDic.ContainsKey(eventType)) if (allEventDic.ContainsKey(eventType))
{ {
Delegate d = normalEventDic[eventType]; Delegate d = allEventDic[eventType];
if (d == null) if (d == null)
{ {
throw new Exception($"移除监听错误:事件{eventType}没有对应的委托"); throw new Exception($"移除监听错误:事件{eventType}没有对应的委托");
} }
else if (d.GetType() != callBack.GetType()) else if (d.GetType() != callback.GetType())
{ {
throw new Exception( throw new Exception(
$"移除监听错误:尝试为事件{eventType}移除不同类型的委托,当前委托类型为{d.GetType()},要移除的委托类型为{callBack.GetType()}"); $"移除监听错误:尝试为事件{eventType}移除不同类型的委托,当前委托类型为{d.GetType()},要移除的委托类型为{callback.GetType()}");
} }
return; return;
} }
@ -64,9 +64,9 @@ public class EventCenter
private static void OnListenerRemoved(string eventType) private static void OnListenerRemoved(string eventType)
{ {
if (normalEventDic[eventType] == null) if (allEventDic[eventType] == null)
{ {
normalEventDic.Remove(eventType); allEventDic.Remove(eventType);
} }
} }
@ -76,66 +76,66 @@ public class EventCenter
/// 无参数监听 /// 无参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener(string eventType, Action callBack) public static void AddListener(string eventType, Action callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action)allEventDic[eventType] + callback;
} }
/// <summary> /// <summary>
/// 一个参数监听 /// 一个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<K>(string eventType, Action<K> callBack) public static void AddListener<K>(string eventType, Action<K> callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action<K>)allEventDic[eventType] + callback;
} }
/// <summary> /// <summary>
/// 两个参数监听 /// 两个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<K, X>(string eventType, Action<K, X> callBack) public static void AddListener<K, X>(string eventType, Action<K, X> callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action<K, X>)allEventDic[eventType] + callback;
} }
/// <summary> /// <summary>
/// 三个参数监听 /// 三个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<K, X, Y>(string eventType, Action<K, X, Y> callBack) public static void AddListener<K, X, Y>(string eventType, Action<K, X, Y> callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action<K, X, Y>)allEventDic[eventType] + callback;
} }
/// <summary> /// <summary>
/// 四个参数监听 /// 四个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack) public static void AddListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action<K, X, Y, Z>)allEventDic[eventType] + callback;
} }
/// <summary> /// <summary>
/// 五个参数监听 /// 五个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack) public static void AddListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callback)
{ {
OnListenerAdding(eventType, callBack); OnListenerAdding(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] + callBack; allEventDic[eventType] = (Action<K, X, Y, Z, W>)allEventDic[eventType] + callback;
} }
@ -146,11 +146,11 @@ public class EventCenter
/// 无参移除监听 /// 无参移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener(string eventType, Action callBack) public static void RemoveListener(string eventType, Action callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
@ -158,11 +158,11 @@ public class EventCenter
/// 一个参数移除监听 /// 一个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<K>(string eventType, Action<K> callBack) public static void RemoveListener<K>(string eventType, Action<K> callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action<K>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
@ -170,11 +170,11 @@ public class EventCenter
/// 两个参数移除监听 /// 两个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<K, X>(string eventType, Action<K, X> callBack) public static void RemoveListener<K, X>(string eventType, Action<K, X> callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action<K, X>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
@ -182,11 +182,11 @@ public class EventCenter
/// 三个参数移除监听 /// 三个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y>(string eventType, Action<K, X, Y> callBack) public static void RemoveListener<K, X, Y>(string eventType, Action<K, X, Y> callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action<K, X, Y>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
@ -194,11 +194,11 @@ public class EventCenter
/// 四个参数移除监听 /// 四个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack) public static void RemoveListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action<K, X, Y, Z>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
@ -206,14 +206,38 @@ public class EventCenter
/// 五个参数移除监听 /// 五个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack) public static void RemoveListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callback)
{ {
OnListenerRemoving(eventType, callBack); OnListenerRemoving(eventType, callback);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] - callBack; allEventDic[eventType] = (Action<K, X, Y, Z, W>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType); OnListenerRemoved(eventType);
} }
public static void RemoveListener(string eventType, Delegate callback)
{
// 如果 dictB 中存在相同的键
if (allEventDic.ContainsKey(eventType))
{
// 获取 MulticastDelegate
MulticastDelegate multicastDelegate = allEventDic[eventType] as MulticastDelegate;
// 移除指定的委托
Delegate[] invocationList = multicastDelegate.GetInvocationList();
Delegate[] newInvocationList = Array.FindAll(invocationList, d => d != callback);
// 更新 dictB 中的委托
if (newInvocationList.Length > 0)
{
allEventDic[eventType] = Delegate.Combine(newInvocationList);
}
else
{
// 如果没有剩余的委托,则从 dictB 中删除该键
allEventDic.Remove(eventType);
}
}
}
//==================================================广播事件======================================================== //==================================================广播事件========================================================
@ -224,11 +248,11 @@ public class EventCenter
public static void BroadCast(string eventType) public static void BroadCast(string eventType)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action callBack) if (d is Action callback)
{ {
callBack(); callback();
} }
else else
{ {
@ -244,11 +268,11 @@ public class EventCenter
public static void BroadCast<K>(string eventType, K arg) public static void BroadCast<K>(string eventType, K arg)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action<K> callBack) if (d is Action<K> callback)
{ {
callBack(arg); callback(arg);
} }
else else
{ {
@ -264,11 +288,11 @@ public class EventCenter
public static void BroadCast<K, X>(string eventType, K arg1, X arg2) public static void BroadCast<K, X>(string eventType, K arg1, X arg2)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action<K, X> callBack) if (d is Action<K, X> callback)
{ {
callBack(arg1, arg2); callback(arg1, arg2);
} }
else else
{ {
@ -284,11 +308,11 @@ public class EventCenter
public static void BroadCast<K, X, Y>(string eventType, K arg1, X arg2, Y arg3) public static void BroadCast<K, X, Y>(string eventType, K arg1, X arg2, Y arg3)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action<K, X, Y> callBack) if (d is Action<K, X, Y> callback)
{ {
callBack(arg1, arg2, arg3); callback(arg1, arg2, arg3);
} }
else else
{ {
@ -304,11 +328,11 @@ public class EventCenter
public static void BroadCast<K, X, Y, Z>(string eventType, K arg1, X arg2, Y arg3, Z arg4) public static void BroadCast<K, X, Y, Z>(string eventType, K arg1, X arg2, Y arg3, Z arg4)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action<K, X, Y, Z> callBack) if (d is Action<K, X, Y, Z> callback)
{ {
callBack(arg1, arg2, arg3, arg4); callback(arg1, arg2, arg3, arg4);
} }
else else
{ {
@ -324,11 +348,11 @@ public class EventCenter
public static void BroadCast<K, X, Y, Z, W>(string eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5) public static void BroadCast<K, X, Y, Z, W>(string eventType, K arg1, X arg2, Y arg3, Z arg4, W arg5)
{ {
Delegate d; Delegate d;
if (normalEventDic.TryGetValue(eventType, out d)) if (allEventDic.TryGetValue(eventType, out d))
{ {
if (d is Action<K, X, Y, Z, W> callBack) if (d is Action<K, X, Y, Z, W> callback)
{ {
callBack(arg1, arg2, arg3, arg4, arg5); callback(arg1, arg2, arg3, arg4, arg5);
} }
else else
{ {
@ -349,66 +373,66 @@ public class EventCenter
/// 无参数监听 /// 无参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T>(T eventType, Action callBack) where T : Enum public static void AddListener<T>(T eventType, Action callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 一个参数监听 /// 一个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T, K>(T eventType, Action<K> callBack) where T : Enum public static void AddListener<T, K>(T eventType, Action<K> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 两个参数监听 /// 两个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum public static void AddListener<T, K, X>(T eventType, Action<K, X> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 三个参数监听 /// 三个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum public static void AddListener<T, K, X, Y>(T eventType, Action<K, X, Y> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 四个参数监听 /// 四个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callBack) where T : Enum public static void AddListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 五个参数监听 /// 五个参数监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void AddListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callBack) where T : Enum public static void AddListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack); AddListener(eventName, callback);
} }
@ -419,66 +443,66 @@ public class EventCenter
/// 无参移除监听 /// 无参移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T>(T eventType, Action callBack) where T : Enum public static void RemoveListener<T>(T eventType, Action callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 一个参数移除监听 /// 一个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T, K>(T eventType, Action<K> callBack) where T : Enum public static void RemoveListener<T, K>(T eventType, Action<K> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 两个参数移除监听 /// 两个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum public static void RemoveListener<T, K, X>(T eventType, Action<K, X> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 三个参数移除监听 /// 三个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum public static void RemoveListener<T, K, X, Y>(T eventType, Action<K, X, Y> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 四个参数移除监听 /// 四个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callBack) where T : Enum public static void RemoveListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
/// <summary> /// <summary>
/// 五个参数移除监听 /// 五个参数移除监听
/// </summary> /// </summary>
/// <param name="eventType">事件</param> /// <param name="eventType">事件</param>
/// <param name="callBack">回调</param> /// <param name="callback">回调</param>
public static void RemoveListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callBack) where T : Enum public static void RemoveListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callback) where T : Enum
{ {
string eventName = $"{eventType.GetType()}_{eventType.ToString()}"; string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack); RemoveListener(eventName, callback);
} }
@ -551,7 +575,8 @@ public class EventCenter
/// </summary> /// </summary>
public static void Clear() public static void Clear()
{ {
normalEventDic.Clear(); allEventDic.Clear();
} }
} }

View File

@ -31,9 +31,15 @@ namespace Ether
} }
} }
protected virtual void OnClear()
{
}
private void OnDestroy() private void OnDestroy()
{ {
EntityManager.Inst.RemoveEntity(this); EntityManager.Inst.RemoveEntity(this);
OnClear();
} }
void OnApplicationQuit() void OnApplicationQuit()

View File

@ -11,6 +11,19 @@ namespace Ether
{ {
public class ImageEx : Image public class ImageEx : Image
{ {
private void OnRectTransformDimensionsChange()
{
base.OnRectTransformDimensionsChange();
Debug.LogError("OnRectTransformDimensionsChange");
}
private void OnTransformChildrenChanged()
{
Debug.LogError("1111111111");
}
#if UNITY_EDITOR #if UNITY_EDITOR
[MenuItem("GameObject/UIEx/ImageEx", priority = -1000)] [MenuItem("GameObject/UIEx/ImageEx", priority = -1000)]

View File

@ -1,12 +1,13 @@
/******************************************************************** /********************************************************************
: FrameBase.cs : FrameBase.cs
: :
: 1982614048@qq.com : 1982614048@qq.com
: 2024/03/29 17:28:19 : 2024/03/29 17:28:19
: :
: 2024/04/04 16:58:56 : 2024/04/04 16:58:56
: :
*********************************************************************/ *********************************************************************/
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -33,7 +34,9 @@ namespace Ether
FrameData = new FrameData(); FrameData = new FrameData();
OpenFrameData = new OpenFrameData(); OpenFrameData = new OpenFrameData();
UIElementAttribute elementAttribute = GetType().GetCustomAttribute<UIElementAttribute>(); UIElementAttribute elementAttribute = GetType().GetCustomAttribute<UIElementAttribute>();
FrameData.PrefabPath = (elementAttribute != null && !string.IsNullOrEmpty(elementAttribute.framePrefabPath)) ? elementAttribute.framePrefabPath : PrefabPath; FrameData.PrefabPath = (elementAttribute != null && !string.IsNullOrEmpty(elementAttribute.framePrefabPath))
? elementAttribute.framePrefabPath
: PrefabPath;
FrameData.Tier = elementAttribute == null ? FrameTier.Middle : elementAttribute.tier; FrameData.Tier = elementAttribute == null ? FrameTier.Middle : elementAttribute.tier;
FrameData.FrameType = elementAttribute == null ? FrameType.Frame : elementAttribute.frameType; FrameData.FrameType = elementAttribute == null ? FrameType.Frame : elementAttribute.frameType;
FrameData.FrameName = GetType().Name; FrameData.FrameName = GetType().Name;
@ -51,14 +54,64 @@ namespace Ether
OnClick(FrameData.Root, OnFocus); OnClick(FrameData.Root, OnFocus);
} }
protected virtual void OnInit() { } /// <summary>
protected virtual void OnOpenAnim() { FrameData.Root?.SetActive(true); } /// 初始化界面
protected virtual void OnShow() { } /// </summary>
protected virtual void OnClose() { } protected virtual void OnInit()
protected virtual void OnSubscribe() { } {
protected virtual void OnUnSubscribe() { } }
protected virtual void OnFocus(PointerEventData pointerEventData) { }
public virtual void OnUpdate(float deltaTime) { } /// <summary>
/// 开启动画
/// </summary>
protected virtual void OnOpenAnim()
{
FrameData.Root?.SetActive(true);
}
/// <summary>
/// 打开界面
/// </summary>
protected virtual void OnShow()
{
}
/// <summary>
/// 关闭界面
/// </summary>
protected virtual void OnClose()
{
}
/// <summary>
/// 注册事件
/// </summary>
protected virtual void OnSubscribe()
{
}
/// <summary>
/// 取消事件注册
/// </summary>
protected virtual void OnUnSubscribe()
{
}
/// <summary>
/// 选中界面
/// </summary>
/// <param name="pointerEventData"></param>
protected virtual void OnFocus(PointerEventData pointerEventData)
{
}
/// <summary>
/// 循环
/// </summary>
/// <param name="deltaTime"></param>
public virtual void OnUpdate(float deltaTime)
{
}
#endregion #endregion
@ -69,8 +122,9 @@ namespace Ether
{ {
if (openFrameData != null) if (openFrameData != null)
{ {
OpenFrameData = openFrameData ; OpenFrameData = openFrameData;
} }
BindRoot(); BindRoot();
FrameData.Root.transform.SetAsLastSibling(); FrameData.Root.transform.SetAsLastSibling();
Debug.Log($"界面{FrameData.FrameName}打开"); Debug.Log($"界面{FrameData.FrameName}打开");
@ -88,6 +142,7 @@ namespace Ether
public void CloseFrame(Action callback = null) public void CloseFrame(Action callback = null)
{ {
Debug.Log($"界面{FrameData.FrameName}关闭"); Debug.Log($"界面{FrameData.FrameName}关闭");
RemoveAllEvent();
OnUnSubscribe(); OnUnSubscribe();
OnClose(); OnClose();
FrameData.Root.SetVisible(false, () => FrameData.Root.SetVisible(false, () =>
@ -107,6 +162,9 @@ namespace Ether
UIManager.Inst.BackFrame(FrameData.FrameName, FrameData.FrameType); UIManager.Inst.BackFrame(FrameData.FrameName, FrameData.FrameType);
} }
/// <summary>
/// 关闭界面
/// </summary>
public void Close() public void Close()
{ {
//如果是子界面的话使用父界面的关闭 //如果是子界面的话使用父界面的关闭
@ -121,6 +179,7 @@ namespace Ether
{ {
UIManager.Inst.CloseFrame(framePair.Key); UIManager.Inst.CloseFrame(framePair.Key);
} }
subFrames.Clear(); subFrames.Clear();
UIManager.Inst.CloseFrame(FrameData.FrameName); UIManager.Inst.CloseFrame(FrameData.FrameName);
} }
@ -134,17 +193,18 @@ namespace Ether
Debug.LogError("打开子界面需要FrameData,并给FrameData中的ParentData赋值"); Debug.LogError("打开子界面需要FrameData,并给FrameData中的ParentData赋值");
return; return;
} }
UIManager.Inst.OpenFrame<T>(openFrameData, callback); UIManager.Inst.OpenFrame<T>(openFrameData, callback);
T frame = UIManager.Inst.GetFrame<T>(); T frame = UIManager.Inst.GetFrame<T>();
subFrames.TryAdd(typeof(T).Name, frame); subFrames.TryAdd(typeof(T).Name, frame);
} }
public void CloseSubFrame<T>(Action callback = null) where T : FrameBase protected void CloseSubFrame<T>(Action callback = null) where T : FrameBase
{ {
CloseSubFrame(typeof(T).Name, callback); CloseSubFrame(typeof(T).Name, callback);
} }
public void CloseSubFrame(string subFrameName, Action callback = null) protected void CloseSubFrame(string subFrameName, Action callback = null)
{ {
subFrames.Remove(subFrameName); subFrames.Remove(subFrameName);
UIManager.Inst.CloseFrame(subFrameName, callback); UIManager.Inst.CloseFrame(subFrameName, callback);
@ -153,6 +213,7 @@ namespace Ether
#endregion #endregion
#region #region
/// <summary> /// <summary>
/// 子物体缓存 /// 子物体缓存
/// </summary> /// </summary>
@ -231,7 +292,7 @@ namespace Ether
protected Transform GetChild(string path) protected Transform GetChild(string path)
{ {
var tempTrans = CheckChildCache(path); var tempTrans = CheckChildCache(path);
if (tempTrans != null) if (tempTrans)
{ {
return tempTrans; return tempTrans;
} }
@ -248,11 +309,181 @@ namespace Ether
//Debug.Log("GetComponent:" + typeof(T) + " : " + path); //Debug.Log("GetComponent:" + typeof(T) + " : " + path);
return GetChild(path).GetComponent<T>(); return GetChild(path).GetComponent<T>();
} }
#endregion
#region
private Dictionary<string, Delegate> eventDic = new Dictionary<string, Delegate>();
private void OnListenerAdding(string eventType, Delegate callback)
{
eventDic.TryAdd(eventType, null);
Delegate d = eventDic[eventType];
if (!string.IsNullOrEmpty(eventType) && d != null && d.GetType() != callback.GetType())
{
throw new Exception(
$"尝试为事件{eventType}添加不同类型的委托,当前事件所对应的委托为{d.GetType()},要添加的委托类型为{callback.GetType()}");
}
}
private void RemoveAllEvent()
{
foreach (var eventPair in eventDic)
{
EventCenter.RemoveListener(eventPair.Key, eventPair.Value);
}
eventDic.Clear();
}
/// <summary>
/// 无参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener(string eventType, Action callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<K>(string eventType, Action<K> callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action<K>)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<K, X>(string eventType, Action<K, X> callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action<K, X>)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<K, X, Y>(string eventType, Action<K, X, Y> callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action<K, X, Y>)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action<K, X, Y, Z>)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callback)
{
OnListenerAdding(eventType, callback);
eventDic[eventType] = (Action<K, X, Y, Z, W>)eventDic[eventType] + callback;
EventCenter.AddListener(eventType, callback);
}
/// <summary>
/// 无参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T>(T eventType, Action callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T, K>(T eventType, Action<K> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T, K, X>(T eventType, Action<K, X> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T, K, X, Y>(T eventType, Action<K, X, Y> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T, K, X, Y, Z>(T eventType, Action<K, X, Y, Z> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callback">回调</param>
protected void AddEventListener<T, K, X, Y, Z, W>(T eventType, Action<K, X, Y, Z, W> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddEventListener(eventName, callback);
}
#endregion #endregion
//========================================= 事件工具 ============================================ //========================================= 事件工具 ============================================
#region #region
protected void OnBtnLeftClick(string path, Action action) protected void OnBtnLeftClick(string path, Action action)
{ {
ButtonEx btn = GetComponent<ButtonEx>(path); ButtonEx btn = GetComponent<ButtonEx>(path);
@ -268,10 +499,7 @@ namespace Ether
protected void OnBtnLeftClick(ButtonEx btn, Action action) protected void OnBtnLeftClick(ButtonEx btn, Action action)
{ {
btn.OnLeftClick.RemoveAllListeners(); btn.OnLeftClick.RemoveAllListeners();
btn.OnLeftClick.AddListener(() => btn.OnLeftClick.AddListener(() => { action?.Invoke(); });
{
action?.Invoke();
});
} }
protected void OnBtnRightClick(string path, Action action) protected void OnBtnRightClick(string path, Action action)
@ -289,14 +517,13 @@ namespace Ether
protected void OnBtnRightClick(ButtonEx btn, Action action) protected void OnBtnRightClick(ButtonEx btn, Action action)
{ {
btn.OnRightClick.RemoveAllListeners(); btn.OnRightClick.RemoveAllListeners();
btn.OnRightClick.AddListener(() => btn.OnRightClick.AddListener(() => { action?.Invoke(); });
{
action?.Invoke();
});
} }
#endregion #endregion
#region #region
protected void SetText(string path, string text) protected void SetText(string path, string text)
{ {
Text label = GetComponent<Text>(path); Text label = GetComponent<Text>(path);
@ -313,9 +540,11 @@ namespace Ether
{ {
label.text = text; label.text = text;
} }
#endregion #endregion
#region #region
protected void SetSprite(string path, string spritePath) protected void SetSprite(string path, string spritePath)
{ {
Image img = GetComponent<Image>(path); Image img = GetComponent<Image>(path);
@ -332,9 +561,11 @@ namespace Ether
{ {
img.sprite = LoaderTools.LoadAsset<Sprite>(spritePath); img.sprite = LoaderTools.LoadAsset<Sprite>(spritePath);
} }
#endregion #endregion
#region #region
protected void SetInputFieldValue(string path, string text) protected void SetInputFieldValue(string path, string text)
{ {
GetComponent<InputField>(path).text = text; GetComponent<InputField>(path).text = text;
@ -361,13 +592,11 @@ namespace Ether
InputField input = obj.GetComponent<InputField>(); InputField input = obj.GetComponent<InputField>();
OnInputFieldValueChanged(input, action); OnInputFieldValueChanged(input, action);
} }
protected void OnInputFieldValueChanged(InputField input, Action<string> action) protected void OnInputFieldValueChanged(InputField input, Action<string> action)
{ {
input.onValueChanged.RemoveAllListeners(); input.onValueChanged.RemoveAllListeners();
input.onValueChanged.AddListener((str) => input.onValueChanged.AddListener((str) => { action?.Invoke(str); });
{
action?.Invoke(str);
});
} }
protected void OnInputFieldEnd(string path, Action<string> action) protected void OnInputFieldEnd(string path, Action<string> action)
@ -384,14 +613,13 @@ namespace Ether
protected void OnInputFieldEnd(InputField input, Action<string> action) protected void OnInputFieldEnd(InputField input, Action<string> action)
{ {
input.onEndEdit.AddListener((str) => input.onEndEdit.AddListener((str) => { action?.Invoke(str); });
{
action?.Invoke(str);
});
} }
#endregion #endregion
#region #region
protected void OnToggleChange(string path, Action<bool> action) protected void OnToggleChange(string path, Action<bool> action)
{ {
Toggle toggle = GetComponent<Toggle>(path); Toggle toggle = GetComponent<Toggle>(path);
@ -406,14 +634,13 @@ namespace Ether
protected void OnToggleChange(Toggle toggle, Action<bool> action) protected void OnToggleChange(Toggle toggle, Action<bool> action)
{ {
toggle.onValueChanged.AddListener((isChange) => toggle.onValueChanged.AddListener((isChange) => { action?.Invoke(isChange); });
{
action?.Invoke(isChange);
});
} }
#endregion #endregion
#region #region
protected void OnSliderChange(string path, Action<float> action) protected void OnSliderChange(string path, Action<float> action)
{ {
Slider slider = GetComponent<Slider>(path); Slider slider = GetComponent<Slider>(path);
@ -428,14 +655,13 @@ namespace Ether
protected void OnSliderChange(Slider slider, Action<float> action) protected void OnSliderChange(Slider slider, Action<float> action)
{ {
slider.onValueChanged.AddListener((value) => slider.onValueChanged.AddListener((value) => { action?.Invoke(value); });
{
action?.Invoke(value);
});
} }
#endregion #endregion
#region EventTrigger事件 #region EventTrigger事件
/// <summary> /// <summary>
/// EventTrigger监听 /// EventTrigger监听
/// </summary> /// </summary>
@ -527,21 +753,23 @@ namespace Ether
/// <param name="dragEndcallback">拖拽结束回调</param> /// <param name="dragEndcallback">拖拽结束回调</param>
internal void OnDragMove(GameObject obj, Action<PointerEventData> dragEndcallback = null) internal void OnDragMove(GameObject obj, Action<PointerEventData> dragEndcallback = null)
{ {
RectTransform canvas = UIManager.Inst.MainCanvas.GetComponent<RectTransform>(); ; //得到canvas的ugui坐标 RectTransform canvas = UIManager.Inst.MainCanvas.GetComponent<RectTransform>();
RectTransform objRect = obj.GetComponent<RectTransform>(); //得到图片的ugui坐标 ; //得到canvas的ugui坐标
Vector2 offset = new Vector3(); //用来得到鼠标和图片的差值 RectTransform objRect = obj.GetComponent<RectTransform>(); //得到图片的ugui坐标
Vector3 imgReduceScale = new Vector3(0.8f, 0.8f, 1); //设置图片缩放 Vector2 offset = new Vector3(); //用来得到鼠标和图片的差值
Vector3 imgNormalScale = new Vector3(1, 1, 1); //正常大小 Vector3 imgReduceScale = new Vector3(0.8f, 0.8f, 1); //设置图片缩放
Vector3 imgNormalScale = new Vector3(1, 1, 1); //正常大小
AddTriggersListener(obj, EventTriggerType.BeginDrag, (BaseEventData) => AddTriggersListener(obj, EventTriggerType.BeginDrag, (BaseEventData) =>
{ {
PointerEventData eventData = (PointerEventData)BaseEventData; PointerEventData eventData = (PointerEventData)BaseEventData;
Vector2 mouseDown = eventData.position; //记录鼠标按下时的屏幕坐标 Vector2 mouseDown = eventData.position; //记录鼠标按下时的屏幕坐标
Vector2 mouseUguiPos = new Vector2(); //定义一个接收返回的ugui坐标 Vector2 mouseUguiPos = new Vector2(); //定义一个接收返回的ugui坐标
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos); bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown,
if (isRect) //如果在 eventData.enterEventCamera, out mouseUguiPos);
if (isRect) //如果在
{ {
//计算图片中心和鼠标点的差值 //计算图片中心和鼠标点的差值
offset = objRect.anchoredPosition - mouseUguiPos; offset = objRect.anchoredPosition - mouseUguiPos;
@ -551,10 +779,11 @@ namespace Ether
AddTriggersListener(obj, EventTriggerType.Drag, (BaseEventData) => AddTriggersListener(obj, EventTriggerType.Drag, (BaseEventData) =>
{ {
PointerEventData eventData = (PointerEventData)BaseEventData; PointerEventData eventData = (PointerEventData)BaseEventData;
Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标 Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标
Vector2 uguiPos = new Vector2(); //用来接收转换后的拖动坐标 Vector2 uguiPos = new Vector2(); //用来接收转换后的拖动坐标
//和上面类似 //和上面类似
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag, eventData.enterEventCamera, out uguiPos); bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDrag,
eventData.enterEventCamera, out uguiPos);
if (isRect) if (isRect)
{ {
@ -563,10 +792,8 @@ namespace Ether
} }
}); });
AddTriggersListener(obj, EventTriggerType.EndDrag, (BaseEventData) => AddTriggersListener(obj, EventTriggerType.EndDrag,
{ (BaseEventData) => { dragEndcallback?.Invoke((PointerEventData)BaseEventData); });
dragEndcallback?.Invoke((PointerEventData)BaseEventData);
});
} }
internal void OnDragMove(string path, Action<PointerEventData> action = null) internal void OnDragMove(string path, Action<PointerEventData> action = null)
@ -574,6 +801,7 @@ namespace Ether
GameObject obj = GetChild(path).gameObject; GameObject obj = GetChild(path).gameObject;
OnDragMove(obj, action); OnDragMove(obj, action);
} }
#endregion #endregion
} }
} }

File diff suppressed because one or more lines are too long

View File

@ -17,7 +17,7 @@ namespace Ether
protected override void OnSubscribe() protected override void OnSubscribe()
{ {
EventCenter.AddListener("CutSceneSucc", CutSceneSucc); AddEventListener("CutSceneSucc", CutSceneSucc);
} }
private void CutSceneSucc() private void CutSceneSucc()
@ -25,9 +25,5 @@ namespace Ether
_AnimatorCrossfade.SetTrigger("FadeExit"); _AnimatorCrossfade.SetTrigger("FadeExit");
} }
protected override void OnUnSubscribe()
{
EventCenter.RemoveListener("CutSceneSucc", CutSceneSucc);
}
} }
} }