- UI基类
BasePanel
负责帮助我门通过代码快速的找到所有的子控件,方便我们在子类中处理逻辑,节约找控件的工作量。
public class BasePanel : MonoBehaviour
{
//通过里式转换原则 来存储所有的控件
private Dictionary<string, List<UIBehaviour>> controlDic = new Dictionary<string, List<UIBehaviour>>();
// Use this for initialization
protected virtual void Awake () {
FindChildrenControl<Button>();
FindChildrenControl<Image>();
FindChildrenControl<Text>();
FindChildrenControl<Toggle>();
FindChildrenControl<Slider>();
FindChildrenControl<ScrollRect>();
FindChildrenControl<InputField>();
}
/// <summary>
/// 显示自己
/// </summary>
public virtual void ShowMe()
{
}
/// <summary>
/// 隐藏自己
/// </summary>
public virtual void HideMe()
{
}
protected virtual void OnClick(string btnName)
{
}
protected virtual void OnValueChanged(string toggleName, bool value)
{
}
/// <summary>
/// 得到对应名字的对应控件脚本
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="controlName"></param>
/// <returns></returns>
protected T GetControl<T>(string controlName) where T : UIBehaviour
{
if(controlDic.ContainsKey(controlName))
{
for( int i = 0; i <controlDic[controlName].Count; ++i )
{
if (controlDic[controlName][i] is T)
return controlDic[controlName][i] as T;
}
}
return null;
}
/// <summary>
/// 找到子对象的对应控件
/// </summary>
/// <typeparam name="T"></typeparam>
private void FindChildrenControl<T>() where T:UIBehaviour
{
T[] controls = this.GetComponentsInChildren<T>();
for (int i = 0; i < controls.Length; ++i)
{
string objName = controls[i].gameObject.name;
if (controlDic.ContainsKey(objName))
controlDic[objName].Add(controls[i]);
else
controlDic.Add(objName, new List<UIBehaviour>() { controls[i] });
//如果是按钮控件
if(controls[i] is Button)
{
(controls[i] as Button).onClick.AddListener(()=>
{
OnClick(objName);
});
}
//如果是单选框或者多选框
else if(controls[i] is Toggle)
{
(controls[i] as Toggle).onValueChanged.AddListener((value) =>
{
OnValueChanged(objName, value);
});
}
}
}
}
- UI管理模块
UIManager
管理所有显示的面板,提供给外部 显示和隐藏等等方法。
/// <summary>
/// UI层级
/// </summary>
public enum E_UI_Layer
{
Bot,
Mid,
Top,
System,
}
/// <summary>
/// UI管理器
/// 1.管理所有显示的面板
/// 2.提供给外部 显示和隐藏等等接口
/// </summary>
public class UIManager : BaseManager<UIManager>
{
public Dictionary<string, BasePanel> panelDic = new Dictionary<string, BasePanel>();
private Transform bot;
private Transform mid;
private Transform top;
private Transform system;
//记录我们UI的Canvas父对象 方便以后外部可能会使用它
public RectTransform canvas;
public UIManager()
{
//创建Canvas 让其过场景的时候 不被移除
GameObject obj = ResMgr.GetInstance().Load<GameObject>("UI/Canvas");
canvas = obj.transform as RectTransform;
GameObject.DontDestroyOnLoad(obj);
//找到各层
bot = canvas.Find("Bot");
mid = canvas.Find("Mid");
top = canvas.Find("Top");
system = canvas.Find("System");
//创建EventSystem 让其过场景的时候 不被移除
obj = ResMgr.GetInstance().Load<GameObject>("UI/EventSystem");
GameObject.DontDestroyOnLoad(obj);
}
/// <summary>
/// 通过层级枚举 得到对应层级的父对象
/// </summary>
/// <param name="layer"></param>
/// <returns></returns>
public Transform GetLayerFather(E_UI_Layer layer)
{
switch(layer)
{
case E_UI_Layer.Bot:
return this.bot;
case E_UI_Layer.Mid:
return this.mid;
case E_UI_Layer.Top:
return this.top;
case E_UI_Layer.System:
return this.system;
}
return null;
}
/// <summary>
/// 显示面板
/// </summary>
/// <typeparam name="T">面板脚本类型</typeparam>
/// <param name="panelName">面板名</param>
/// <param name="layer">显示在哪一层</param>
/// <param name="callBack">当面板预设体创建成功后 你想做的事</param>
public void ShowPanel<T>(string panelName, E_UI_Layer layer = E_UI_Layer.Mid, UnityAction<T> callBack = null) where T:BasePanel
{
if (panelDic.ContainsKey(panelName))
{
panelDic[panelName].ShowMe();
// 处理面板创建完成后的逻辑
if (callBack != null)
callBack(panelDic[panelName] as T);
//避免面板重复加载 如果存在该面板 即直接显示 调用回调函数后 直接return 不再处理后面的异步加载逻辑
return;
}
ResMgr.GetInstance().LoadAsync<GameObject>("UI/" + panelName, (obj) =>
{
//把他作为 Canvas的子对象
//并且 要设置它的相对位置
//找到父对象 你到底显示在哪一层
Transform father = bot;
switch(layer)
{
case E_UI_Layer.Mid:
father = mid;
break;
case E_UI_Layer.Top:
father = top;
break;
case E_UI_Layer.System:
father = system;
break;
}
//设置父对象 设置相对位置和大小
obj.transform.SetParent(father);
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one;
(obj.transform as RectTransform).offsetMax = Vector2.zero;
(obj.transform as RectTransform).offsetMin = Vector2.zero;
//得到预设体身上的面板脚本
T panel = obj.GetComponent<T>();
// 处理面板创建完成后的逻辑
if (callBack != null)
callBack(panel);
panel.ShowMe();
//把面板存起来
panelDic.Add(panelName, panel);
});
}
/// <summary>
/// 隐藏面板
/// </summary>
/// <param name="panelName"></param>
public void HidePanel(string panelName)
{
if(panelDic.ContainsKey(panelName))
{
panelDic[panelName].HideMe();
GameObject.Destroy(panelDic[panelName].gameObject);
panelDic.Remove(panelName);
}
}
/// <summary>
/// 得到某一个已经显示的面板 方便外部使用
/// </summary>
public T GetPanel<T>(string name) where T:BasePanel
{
if (panelDic.ContainsKey(name))
return panelDic[name] as T;
return null;
}
/// <summary>
/// 给控件添加自定义事件监听
/// </summary>
/// <param name="control">控件对象</param>
/// <param name="type">事件类型</param>
/// <param name="callBack">事件的响应函数</param>
public static void AddCustomEventListener(UIBehaviour control, EventTriggerType type, UnityAction<BaseEventData> callBack)
{
EventTrigger trigger = control.GetComponent<EventTrigger>();
if (trigger == null)
trigger = control.gameObject.AddComponent<EventTrigger>();
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = type;
entry.callback.AddListener(callBack);
trigger.triggers.Add(entry);
}
}
- 测试代码
public class LoginPanel : BasePanel
{
public void InitData() {
Debug.Log("初始化信息!");
}
// Start is called before the first frame update
void Start()
{
//GetControl<Button>("btnLogin").onClick.AddListener(()=> {
// Debug.Log("我被点击了");
//});
// GetControl<Button>("btnExit").onClick.AddListener(()=> {
// Debug.Log("我要退出了");
// this.gameObject.SetActive(false);
// });
UIManager.AddCustomEventListener(GetControl<Button>("btnExit"), EventTriggerType.PointerEnter, (data) =>
{
Debug.Log("我要进入了");
});
UIManager.AddCustomEventListener(GetControl<Button>("btnExit"), EventTriggerType.PointerExit, (data) =>
{
Debug.Log("我要退出了");
});
}
protected override void OnClick(string btnName)
{
switch (btnName)
{
case "btnLogin":
Debug.Log("我被点击了");
break;
case "btnExit":
Debug.Log("我要退出了");
break;
default:
break;
}
}
public override void ShowMe()
{
Debug.Log("我显示出来了!");
}
public override void HideMe()
{
Debug.Log("我要隐藏了!");
}
// Update is called once per frame
void Update()
{
}
}
public class UITest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
UIManager.GetInstance().ShowPanel<LoginPanel>("LoginPanel", E_UI_Layer.Mid, showPanel);
}
void showPanel(LoginPanel loginPanel)
{
loginPanel.InitData();
}
// Update is called once per frame
void Update()
{
}
}
- 脚本挂载
建立两个预制体,Canvas
预制体和LoginPanel
预制体,Canvas
预制体结构如图所示。分别有四个不同层级panel。
LoginPanel
预制体结构如图所示。
- 测试结果,初始化信息的调用,按钮的点击事件以及自定义的鼠标进入与滑出事件都得到了响应。
UIManager脚本引用的资源加载模块。粘贴时注意查看此系列文章。
关于如何设计UI的可以看下文章下方的推荐书籍,想要改善代码质量的程序员及技术经理看看代码整洁之道吧!!!
链接:Unity程序开发框架——单例模式基类模块.Unity程序开发框架——缓存池模块.Unity程序开发框架——事件中心模块.Unity程序开发框架——公共Mono模块.Unity程序开发框架——场景切换模块.Unity程序开发框架——资源加载模块.Unity程序开发框架——输入控制模块.Unity程序开发框架——UI管理模块.