添加界面事件处理

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

View File

@ -28,33 +28,33 @@ public class EventCenter
{
#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)
{
throw new Exception($"移除监听错误:事件{eventType}没有对应的委托");
}
else if (d.GetType() != callBack.GetType())
else if (d.GetType() != callback.GetType())
{
throw new Exception(
$"移除监听错误:尝试为事件{eventType}移除不同类型的委托,当前委托类型为{d.GetType()},要移除的委托类型为{callBack.GetType()}");
$"移除监听错误:尝试为事件{eventType}移除不同类型的委托,当前委托类型为{d.GetType()},要移除的委托类型为{callback.GetType()}");
}
return;
}
@ -64,9 +64,9 @@ public class EventCenter
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>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener(string eventType, Action callBack)
/// <param name="callback">回调</param>
public static void AddListener(string eventType, Action callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action)allEventDic[eventType] + callback;
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K>(string eventType, Action<K> callBack)
/// <param name="callback">回调</param>
public static void AddListener<K>(string eventType, Action<K> callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action<K>)allEventDic[eventType] + callback;
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X>(string eventType, Action<K, X> callBack)
/// <param name="callback">回调</param>
public static void AddListener<K, X>(string eventType, Action<K, X> callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action<K, X>)allEventDic[eventType] + callback;
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y>(string eventType, Action<K, X, Y> callBack)
/// <param name="callback">回调</param>
public static void AddListener<K, X, Y>(string eventType, Action<K, X, Y> callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y>)allEventDic[eventType] + callback;
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack)
/// <param name="callback">回调</param>
public static void AddListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y, Z>)allEventDic[eventType] + callback;
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack)
/// <param name="callback">回调</param>
public static void AddListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callback)
{
OnListenerAdding(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] + callBack;
OnListenerAdding(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y, Z, W>)allEventDic[eventType] + callback;
}
@ -146,11 +146,11 @@ public class EventCenter
/// 无参移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener(string eventType, Action callBack)
/// <param name="callback">回调</param>
public static void RemoveListener(string eventType, Action callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action)allEventDic[eventType] - callback;
OnListenerRemoved(eventType);
}
@ -158,11 +158,11 @@ public class EventCenter
/// 一个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K>(string eventType, Action<K> callBack)
/// <param name="callback">回调</param>
public static void RemoveListener<K>(string eventType, Action<K> callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K>)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action<K>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType);
}
@ -170,11 +170,11 @@ public class EventCenter
/// 两个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X>(string eventType, Action<K, X> callBack)
/// <param name="callback">回调</param>
public static void RemoveListener<K, X>(string eventType, Action<K, X> callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X>)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action<K, X>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType);
}
@ -182,11 +182,11 @@ public class EventCenter
/// 三个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y>(string eventType, Action<K, X, Y> callBack)
/// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y>(string eventType, Action<K, X, Y> callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y>)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType);
}
@ -194,11 +194,11 @@ public class EventCenter
/// 四个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callBack)
/// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y, Z>(string eventType, Action<K, X, Y, Z> callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z>)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y, Z>)allEventDic[eventType] - callback;
OnListenerRemoved(eventType);
}
@ -206,14 +206,38 @@ public class EventCenter
/// 五个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callBack)
/// <param name="callback">回调</param>
public static void RemoveListener<K, X, Y, Z, W>(string eventType, Action<K, X, Y, Z, W> callback)
{
OnListenerRemoving(eventType, callBack);
normalEventDic[eventType] = (Action<K, X, Y, Z, W>)normalEventDic[eventType] - callBack;
OnListenerRemoving(eventType, callback);
allEventDic[eventType] = (Action<K, X, Y, Z, W>)allEventDic[eventType] - callback;
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)
{
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
{
@ -244,11 +268,11 @@ public class EventCenter
public static void BroadCast<K>(string eventType, K arg)
{
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
{
@ -264,11 +288,11 @@ public class EventCenter
public static void BroadCast<K, X>(string eventType, K arg1, X arg2)
{
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
{
@ -284,11 +308,11 @@ public class EventCenter
public static void BroadCast<K, X, Y>(string eventType, K arg1, X arg2, Y arg3)
{
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
{
@ -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)
{
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
{
@ -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)
{
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
{
@ -349,66 +373,66 @@ public class EventCenter
/// 无参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T>(T eventType, Action callBack) where T : Enum
/// <param name="callback">回调</param>
public static void AddListener<T>(T eventType, Action callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
/// <summary>
/// 一个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K>(T eventType, Action<K> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void AddListener<T, K>(T eventType, Action<K> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
/// <summary>
/// 两个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void AddListener<T, K, X>(T eventType, Action<K, X> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
/// <summary>
/// 三个参数监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void AddListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void AddListener<T, K, X, Y>(T eventType, Action<K, X, Y> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
/// <summary>
/// 四个参数监听
/// </summary>
/// <param name="eventType">事件</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
/// <param name="callback">回调</param>
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()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
/// <summary>
/// 五个参数监听
/// </summary>
/// <param name="eventType">事件</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
/// <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
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
AddListener(eventName, callBack);
AddListener(eventName, callback);
}
@ -419,66 +443,66 @@ public class EventCenter
/// 无参移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T>(T eventType, Action callBack) where T : Enum
/// <param name="callback">回调</param>
public static void RemoveListener<T>(T eventType, Action callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
/// <summary>
/// 一个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K>(T eventType, Action<K> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void RemoveListener<T, K>(T eventType, Action<K> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
/// <summary>
/// 两个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X>(T eventType, Action<K, X> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void RemoveListener<T, K, X>(T eventType, Action<K, X> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
/// <summary>
/// 三个参数移除监听
/// </summary>
/// <param name="eventType">事件</param>
/// <param name="callBack">回调</param>
public static void RemoveListener<T, K, X, Y>(T eventType, Action<K, X, Y> callBack) where T : Enum
/// <param name="callback">回调</param>
public static void RemoveListener<T, K, X, Y>(T eventType, Action<K, X, Y> callback) where T : Enum
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
/// <summary>
/// 四个参数移除监听
/// </summary>
/// <param name="eventType">事件</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
/// <param name="callback">回调</param>
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()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
/// <summary>
/// 五个参数移除监听
/// </summary>
/// <param name="eventType">事件</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
/// <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
{
string eventName = $"{eventType.GetType()}_{eventType.ToString()}";
RemoveListener(eventName, callBack);
RemoveListener(eventName, callback);
}
@ -551,7 +575,8 @@ public class EventCenter
/// </summary>
public static void Clear()
{
normalEventDic.Clear();
}
allEventDic.Clear();
}
}

View File

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

View File

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

View File

@ -7,6 +7,7 @@
: 2024/04/04 16:58:56
:
*********************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
@ -33,7 +34,9 @@ namespace Ether
FrameData = new FrameData();
OpenFrameData = new OpenFrameData();
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.FrameType = elementAttribute == null ? FrameType.Frame : elementAttribute.frameType;
FrameData.FrameName = GetType().Name;
@ -51,14 +54,64 @@ namespace Ether
OnClick(FrameData.Root, OnFocus);
}
protected virtual void OnInit() { }
protected virtual void OnOpenAnim() { FrameData.Root?.SetActive(true); }
protected virtual void OnShow() { }
protected virtual void OnClose() { }
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 OnInit()
{
}
/// <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
@ -71,6 +124,7 @@ namespace Ether
{
OpenFrameData = openFrameData;
}
BindRoot();
FrameData.Root.transform.SetAsLastSibling();
Debug.Log($"界面{FrameData.FrameName}打开");
@ -88,6 +142,7 @@ namespace Ether
public void CloseFrame(Action callback = null)
{
Debug.Log($"界面{FrameData.FrameName}关闭");
RemoveAllEvent();
OnUnSubscribe();
OnClose();
FrameData.Root.SetVisible(false, () =>
@ -107,6 +162,9 @@ namespace Ether
UIManager.Inst.BackFrame(FrameData.FrameName, FrameData.FrameType);
}
/// <summary>
/// 关闭界面
/// </summary>
public void Close()
{
//如果是子界面的话使用父界面的关闭
@ -121,6 +179,7 @@ namespace Ether
{
UIManager.Inst.CloseFrame(framePair.Key);
}
subFrames.Clear();
UIManager.Inst.CloseFrame(FrameData.FrameName);
}
@ -134,17 +193,18 @@ namespace Ether
Debug.LogError("打开子界面需要FrameData,并给FrameData中的ParentData赋值");
return;
}
UIManager.Inst.OpenFrame<T>(openFrameData, callback);
T frame = UIManager.Inst.GetFrame<T>();
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);
}
public void CloseSubFrame(string subFrameName, Action callback = null)
protected void CloseSubFrame(string subFrameName, Action callback = null)
{
subFrames.Remove(subFrameName);
UIManager.Inst.CloseFrame(subFrameName, callback);
@ -153,6 +213,7 @@ namespace Ether
#endregion
#region
/// <summary>
/// 子物体缓存
/// </summary>
@ -231,7 +292,7 @@ namespace Ether
protected Transform GetChild(string path)
{
var tempTrans = CheckChildCache(path);
if (tempTrans != null)
if (tempTrans)
{
return tempTrans;
}
@ -248,11 +309,181 @@ namespace Ether
//Debug.Log("GetComponent:" + typeof(T) + " : " + path);
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
//========================================= 事件工具 ============================================
#region
protected void OnBtnLeftClick(string path, Action action)
{
ButtonEx btn = GetComponent<ButtonEx>(path);
@ -268,10 +499,7 @@ namespace Ether
protected void OnBtnLeftClick(ButtonEx btn, Action action)
{
btn.OnLeftClick.RemoveAllListeners();
btn.OnLeftClick.AddListener(() =>
{
action?.Invoke();
});
btn.OnLeftClick.AddListener(() => { action?.Invoke(); });
}
protected void OnBtnRightClick(string path, Action action)
@ -289,14 +517,13 @@ namespace Ether
protected void OnBtnRightClick(ButtonEx btn, Action action)
{
btn.OnRightClick.RemoveAllListeners();
btn.OnRightClick.AddListener(() =>
{
action?.Invoke();
});
btn.OnRightClick.AddListener(() => { action?.Invoke(); });
}
#endregion
#region
protected void SetText(string path, string text)
{
Text label = GetComponent<Text>(path);
@ -313,9 +540,11 @@ namespace Ether
{
label.text = text;
}
#endregion
#region
protected void SetSprite(string path, string spritePath)
{
Image img = GetComponent<Image>(path);
@ -332,9 +561,11 @@ namespace Ether
{
img.sprite = LoaderTools.LoadAsset<Sprite>(spritePath);
}
#endregion
#region
protected void SetInputFieldValue(string path, string text)
{
GetComponent<InputField>(path).text = text;
@ -361,13 +592,11 @@ namespace Ether
InputField input = obj.GetComponent<InputField>();
OnInputFieldValueChanged(input, action);
}
protected void OnInputFieldValueChanged(InputField input, Action<string> action)
{
input.onValueChanged.RemoveAllListeners();
input.onValueChanged.AddListener((str) =>
{
action?.Invoke(str);
});
input.onValueChanged.AddListener((str) => { action?.Invoke(str); });
}
protected void OnInputFieldEnd(string path, Action<string> action)
@ -384,14 +613,13 @@ namespace Ether
protected void OnInputFieldEnd(InputField input, Action<string> action)
{
input.onEndEdit.AddListener((str) =>
{
action?.Invoke(str);
});
input.onEndEdit.AddListener((str) => { action?.Invoke(str); });
}
#endregion
#region
protected void OnToggleChange(string path, Action<bool> action)
{
Toggle toggle = GetComponent<Toggle>(path);
@ -406,14 +634,13 @@ namespace Ether
protected void OnToggleChange(Toggle toggle, Action<bool> action)
{
toggle.onValueChanged.AddListener((isChange) =>
{
action?.Invoke(isChange);
});
toggle.onValueChanged.AddListener((isChange) => { action?.Invoke(isChange); });
}
#endregion
#region
protected void OnSliderChange(string path, Action<float> action)
{
Slider slider = GetComponent<Slider>(path);
@ -428,14 +655,13 @@ namespace Ether
protected void OnSliderChange(Slider slider, Action<float> action)
{
slider.onValueChanged.AddListener((value) =>
{
action?.Invoke(value);
});
slider.onValueChanged.AddListener((value) => { action?.Invoke(value); });
}
#endregion
#region EventTrigger事件
/// <summary>
/// EventTrigger监听
/// </summary>
@ -527,7 +753,8 @@ namespace Ether
/// <param name="dragEndcallback">拖拽结束回调</param>
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>();
; //得到canvas的ugui坐标
RectTransform objRect = obj.GetComponent<RectTransform>(); //得到图片的ugui坐标
Vector2 offset = new Vector3(); //用来得到鼠标和图片的差值
Vector3 imgReduceScale = new Vector3(0.8f, 0.8f, 1); //设置图片缩放
@ -540,7 +767,8 @@ namespace Ether
Vector2 mouseDown = eventData.position; //记录鼠标按下时的屏幕坐标
Vector2 mouseUguiPos = new Vector2(); //定义一个接收返回的ugui坐标
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown, eventData.enterEventCamera, out mouseUguiPos);
bool isRect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, mouseDown,
eventData.enterEventCamera, out mouseUguiPos);
if (isRect) //如果在
{
//计算图片中心和鼠标点的差值
@ -554,7 +782,8 @@ namespace Ether
Vector2 mouseDrag = eventData.position; //当鼠标拖动时的屏幕坐标
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)
{
@ -563,10 +792,8 @@ namespace Ether
}
});
AddTriggersListener(obj, EventTriggerType.EndDrag, (BaseEventData) =>
{
dragEndcallback?.Invoke((PointerEventData)BaseEventData);
});
AddTriggersListener(obj, EventTriggerType.EndDrag,
(BaseEventData) => { dragEndcallback?.Invoke((PointerEventData)BaseEventData); });
}
internal void OnDragMove(string path, Action<PointerEventData> action = null)
@ -574,6 +801,7 @@ namespace Ether
GameObject obj = GetChild(path).gameObject;
OnDragMove(obj, action);
}
#endregion
}
}

File diff suppressed because one or more lines are too long

View File

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