http://www.lxweimin.com/p/666de6c7695a
之前這個在原來看來不是太完善所以重構下
代碼只有在不斷地重構(維護)下才會變得越來越好
根據最后項目地址下載后 創建一個新物體
image.png
這個代碼放到空物體上就可以運行了
image.png
運用特性尋找UI預制體路徑
之前加載窗口預制體 是根據這個表加載的 每次都要切進去添加很麻煩
image.png
因為我們的路徑和名字都有規律可尋 但是要保證預制體和腳本名一樣
在這個常量類中定義 把之前UIConfig類改掉
image.png
image.png
改為這樣
image.png
然后這些緩存路徑都可以刪了
image.png
然后運行 沒問題 可以把UI json也刪了
消息分發優化
就是MessageCenter 這個類
用的是obj類型 在轉換值類型會裝箱拆箱
換成之前寫的分發腳本
http://www.lxweimin.com/p/02e262d60efd
之前是這么傳遞的
image.png
image.png
當然要把枚舉改為string 字符串
差不多這樣改好了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
//回調管理 不用Action因為自帶的最多4個參數
public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);
/// <summary>
/// 事件管理
/// </summary>
public class EventCenter
{
static Dictionary<string, Delegate> m_EventTable = new Dictionary<string, Delegate>();
static void OnListenerAdding(string eventType, Delegate callBack)
{
if (!m_EventTable.ContainsKey(eventType))
{
m_EventTable.Add(eventType, null);
}
Delegate d = m_EventTable[eventType];
if (d != null && d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("嘗試為事件{0}添加不同類型的委托,當前事件對應的的委托是{1},要添加的委托類型是{2}", eventType, d.GetType(), callBack));
}
}
static void OnListenerRemoving(string eventType, Delegate callBack)
{
if (m_EventTable.ContainsKey(eventType))
{
Delegate d = m_EventTable[eventType];
if (d == null)
{
throw new Exception(string.Format("移除監聽錯誤:事件{0}沒有對應的委托", eventType));
}
else if (d.GetType() != callBack.GetType())
{
throw new Exception(string.Format("移除監聽錯誤:嘗試為事件{0}移除不同類型的委托,當前委托類型為{1}" +
",要移除的委托的類型為{2}", eventType, d.GetType(), callBack.GetType()));
}
}
else
{
throw new Exception(string.Format("移除監聽錯誤:沒有事件碼{0}", eventType));
}
}
static void OnListenerRemoved(string eventType)
{
if (m_EventTable[eventType] == null)
{
m_EventTable.Remove(eventType);
}
}
/// <summary>
/// 無參添加監聽的方法
/// </summary>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void AddListener(string eventType, CallBack callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 1參添加監聽的方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void AddListener<T>(string eventType, CallBack<T> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 2參添加監聽的方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="X"></typeparam>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void AddListener<T, X>(string eventType, CallBack<T, X> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
}
public static void AddListener<T, X, Y>(string eventType, CallBack<T, X, Y> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
}
public static void AddListener<T, X, Y, Z>(string eventType, CallBack<T, X, Y, Z> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
}
public static void AddListener<T, X, Y, Z, W>(string eventType, CallBack<T, X, Y, Z, W> callBack)
{
OnListenerAdding(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
}
/// <summary>
/// 移除某個類型所有監聽
/// </summary>
/// <param name="eventType"></param>
public static void RemoveAllListener(string eventType)
{
if (m_EventTable.ContainsKey(eventType))
{
m_EventTable.Remove(eventType);
}
else
{
throw new Exception(string.Format("移除監聽錯誤:沒有事件碼{0}", eventType));
}
}
/// <summary>
/// 移除無參監聽的方法
/// </summary>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void RemoveListener(string eventType, CallBack callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 移除1參監聽的方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void RemoveListener<T>(string eventType, CallBack<T> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 移除2參監聽的方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="eventType"></param>
/// <param name="callBack"></param>
public static void RemoveListener<T, X>(string eventType, CallBack<T, X> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
public static void RemoveListener<T, X, Y>(string eventType, CallBack<T, X, Y> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
public static void RemoveListener<T, X, Y, Z>(string eventType, CallBack<T, X, Y, Z> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
public static void RemoveListener<T, X, Y, Z, W>(string eventType, CallBack<T, X, Y, Z, W> callBack)
{
OnListenerRemoving(eventType, callBack);
m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
OnListenerRemoved(eventType);
}
/// <summary>
/// 廣播無參方法
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast(string eventType)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack callBack = d as CallBack;
if (callBack != null)
{
callBack();
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
/// <summary>
/// 廣播1參方法
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T>(string eventType, T arg)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T> callBack = d as CallBack<T>;
if (callBack != null)
{
callBack(arg);
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
/// <summary>
/// 廣播2參方法
/// </summary>
/// <param name="eventType"></param>
public static void Broadcast<T, X>(string eventType, T arg1, X arg2)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X> callBack = d as CallBack<T, X>;
if (callBack != null)
{
callBack(arg1, arg2);
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
public static void Broadcast<T, X, Y>(string eventType, T arg1, X arg2, Y arg3)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
if (callBack != null)
{
callBack(arg1, arg2, arg3);
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
public static void Broadcast<T, X, Y, Z>(string eventType, T arg1, X arg2, Y arg3, Z arg4)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4);
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
public static void Broadcast<T, X, Y, Z, W>(string eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
{
Delegate d;
if (m_EventTable.TryGetValue(eventType, out d))
{
CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
if (callBack != null)
{
callBack(arg1, arg2, arg3, arg4, arg5);
}
else
{
throw new Exception(string.Format("廣播事件錯誤:事件{0}對應的委托具有不同類型", eventType));
}
}
}
}
然后BaseUI這兩個基本可以棄用了 不然要寫6遍一樣的
image.png
然后把MessageCenter類刪掉
報錯的代碼這么改
那個list的改為這個 不然會種類不同報錯
image.png
image.png
然后是接收代碼
image.png
image.png
image.png
正常運行