using System;
using System.Collections.Generic;
using UnityEngine;
namespace Framework
{
/// <summary>
/// 基本面板类
/// </summary>
public abstract class UIPanel
{
private const string LOG_GET = "{0} can't get{1} component:{2}";
/// <summary>
/// 游戏物体
/// </summary>
public GameObject GameObject { get; private set; }
/// <summary>
/// 世界变换
/// </summary>
public RectTransform Transform { get { return GameObject.transform as RectTransform; } }
/// <summary>
/// 面板名
/// </summary>
public string PanelName { get; private set; }
public string Name
{
get { return GameObject.name; }
set { GameObject.name = value; }
}
/// <summary>
/// 启用开关
/// </summary>
public virtual bool Enabled
{
get { return GameObject.activeSelf; }
set
{
GameObject.SetActive(value);
if (value)
{
OnEnabled();
}
else
{
OnDisabled();
}
}
}
/// <summary>
/// 面板链表
/// </summary>
protected List<UIPanel> m_PanelList = new List<UIPanel>();
/// <summary>
/// 动态列表
/// </summary>
protected List<UIPanel> m_OpenList = new List<UIPanel>();
/// <summary>
/// 获取面板数量
/// </summary>
public int PanelCount { get { return m_PanelList.Count; } }
/// <summary>
/// 初始化
/// </summary>
/// <param name="panelName"></param>
/// <param name="gameObject"></param>
public void Init(string panelName, GameObject gameObject)
{
PanelName = panelName;
GameObject = gameObject;
OnInit();
}
/// <summary>
/// 初始化回调
/// </summary>
public virtual void OnInit()
{
}
/// <summary>
/// 启用调用
/// </summary>
public virtual void OnEnabled()
{
foreach (var panel in m_PanelList)
{
if (panel.Enabled)
{
if(!m_OpenList.Equals(panel))
m_OpenList.Add(panel);
panel.OnEnabled();
}
}
}
/// <summary>
/// 关闭调用
/// </summary>
public virtual void OnDisabled()
{
foreach (var panel in m_PanelList)
{
if(m_OpenList.Equals(panel))
m_OpenList.Remove(panel);
panel.OnDisabled();
}
}
/// <summary>
/// 关闭顶部面板
/// </summary>
public virtual void CloseTopPanel()
{
if (m_OpenList == null || m_OpenList.Count <= 0)
return;
m_OpenList[m_OpenList.Count-1].Enabled = false;
m_OpenList.RemoveAt(m_OpenList.Count - 1);
}
/// <summary>
/// 打开面板
/// </summary>
/// <param name="panel">指定面板</param>
public void OpenPanel(UIPanel panel)
{
m_OpenList.Add(panel);
panel.Enabled = true;
}
/// <summary>
/// 更新回调
/// </summary>
public virtual void OnUpdate()
{
foreach (var panel in m_PanelList)
{
if (!panel.Enabled)
continue;
panel.OnUpdate();
}
}
/// <summary>
/// 销毁回调
/// </summary>
public virtual void OnDestroy()
{
foreach (var panel in m_PanelList)
{
panel.OnDestroy();
}
m_PanelList.Clear();
m_OpenList.Clear();
}
/// <summary>
/// 获取所有面板
/// </summary>
/// <returns>返回所有面板</returns>
public UIPanel[] GetAllPanel()
{
return m_PanelList.ToArray();
}
/// <summary>
/// 获取节点
/// </summary>
/// <param name="childName">节点名</param>
/// <returns>返回节点</returns>
public Transform GetChild(string childName = null)
{
Transform child = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
if (child == null)
{
Debug.LogError(string.Format(LOG_GET, Name, "child", childName));
}
return child;
}
/// <summary>
/// 获取面板
/// </summary>
/// <param name="type">面板类型</param>
/// <param name="childName">面板节点名</param>
/// <param name="panelName">面板名 </param>
/// <returns>返回面板</returns>
public UIPanel GetPanel(Type type, string childName = null, string panelName = null)
{
Transform child = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
if (child == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
return null;
}
UIPanel panel = Activator.CreateInstance(type) as UIPanel;
panel.Init(panelName ?? panelName, child.gameObject);
m_PanelList.Add(panel);
return panel;
}
/// <summary>
/// 获取动态面板
/// </summary>
/// <param name="type">动态面板类型</param>
/// <param name="panelName">面板资源名</param>
/// <param name="childName">面板节点名</param>
/// <returns>返回面板</returns>
public UIDynamicPanel GetDynamicPanel(Type type, string panelName, string childName = null)
{
Transform child = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
if (child == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
return null;
}
GameObject asset = Resources.Load<GameObject>(panelName);
UIDynamicPanel panel = new UIDynamicPanel(type, asset);
panel.Init(panelName, child.gameObject);
m_PanelList.Add(panel);
return panel;
}
/// <summary>
/// 获取组件
/// </summary>
/// <param name="type">组件类型</param>
/// <param name="childName">节点名</param>
/// <returns>返回组件</returns>
public Component GetComponent(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
if (transform == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
Component component = transform.GetComponent(type);
if (component == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return component;
}
/// <summary>
/// 从子节点获取组件
/// </summary>
/// <param name="type"></param>
/// <param name="childName"></param>
/// <returns>返回组件</returns>
public Component GetComponentInChildren(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
Component component = transform.GetComponentInChildren(type, true);
if (component == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return component;
}
/// <summary>
/// 从父节点获取组件
/// </summary>
/// <param name="type">组件类型</param>
/// <param name="childName">节点名</param>
/// <returns>返回组件</returns>
public Component GetComponentInParent(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
Component component = transform.GetComponentInParent(type);
if (component == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return component;
}
/// <summary>
/// 获取类型的所有组件
/// </summary>
/// <param name="type">组件类型</param>
/// <param name="childName">节点名</param>
/// <returns>返回组件</returns>
public Component[] GetComponents(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
Component[] components = transform.GetComponents(type);
if (components == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return components;
}
/// <summary>
/// 从子节点获取所有组件
/// </summary>
/// <param name="type">组件类型</param>
/// <param name="childName">节点名</param>
/// <returns>返回组件</returns>
public Component[] GetComponentsInChildren(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
Component[] components = transform.GetComponentsInChildren(type, true);
if (components == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return components;
}
/// <summary>
/// 从父节点获取所有组件
/// </summary>
/// <param name="type"></param>
/// <param name="childName"></param>
/// <returns></returns>
public Component[] GetComponentsInParent(Type type, string childName = null)
{
Transform transform = string.IsNullOrEmpty(childName) ? Transform : Transform.Find(childName);
Component[] components = transform.GetComponentsInParent(type, true);
if (components == null)
{
Debug.LogError(string.Format(LOG_GET, Name, type.Name, childName));
}
return components;
}
}
}