UI框架(1)
1.大致结构
2.UIType
IsClearStack:判断这个UI是否需要清空栈上的UI
UIFormType,判断这个UI的类型,来决定他在哪个父物体上面,是什么显示方法,是什么关闭方法
显示类型:判断这个UI的显示类型,显示的时候会对周围造成什么影响
UIFormLucenyType:当UI是弹出类型的时候,来判断是什么透明度
3.UIFormBase
4.UIManager
注意,重点来了,之前的都是为了这个做铺垫
这个是UI框架的各个节点,用处如下
Normal:UIType.UIForms_Type=Norm的时候这个就是父物体
Fixed:同上
PopUp:同上,这里有一个遮罩,后面讲
UIScripts:UI脚本都会自动存放在这里,方便管理
UICamera:UI相机
第一步,先找到各个节点,单例,还有要使用的字典,栈
这个很重要,所以放下面给个重点提示!!!!!其实干嘛的注释里面有了,接下来就是围绕这些存储数据做文章了
//UI根节点
[SerializeField]
private Transform _TraCanvasTransform = null;
//全屏幕显示的节点
[SerializeField]
private Transform _TraNormal = null;
//固定显示的节点
[SerializeField]
private Transform _TraFixed = null;
//弹出节点
[SerializeField]
private Transform _TraPopUp = null;
//UI管理脚本的节点
[SerializeField]
private Transform _TraUIScripts = null;
#endregion
#region 单例
private static UIManager _Instance = null;
public static UIManager Instance
{
get
{
if (_Instance == null)
{
_Instance = new GameObject("_UIManager").AddComponent<UIManager>();
}
return _Instance;
}
}
#endregion
#region UI字典
//UI窗体预设路径(参数1:窗体预设名称,2:表示窗体预设路径)
private Dictionary<string, string> _DicFormsPaths = new Dictionary<string, string>();
//缓存所有UI窗体
private Dictionary<string, BaseUIForm> _DicALLUIForms;
//当前显示的UI窗体
private Dictionary<string, BaseUIForm> _DicCurrentShowUIForms;
#endregion
//用来存放Fixed窗口
private Stack<BaseUIForm> _StaCurrentUIForms;
private void Awake()
{
_DicCurrentShowUIForms = new Dictionary<string, BaseUIForm>();
_DicFormsPaths = new Dictionary<string, string>();
_DicALLUIForms = new Dictionary<string, BaseUIForm>();
_StaCurrentUIForms = new Stack<BaseUIForm>();
InitRootCanvasLoading();
_TraCanvasTransform = GameObject.FindGameObjectWithTag(SysDefine.UI_Tag_Canvas).transform;
_TraNormal = _TraCanvasTransform.Find(SysDefine.UI_TraNormal);
_TraFixed = _TraCanvasTransform.Find(SysDefine.UI_TraFixed);
_TraPopUp = _TraCanvasTransform.Find(SysDefine.UI_TraPopUp);
_TraUIScripts = _TraCanvasTransform.Find(SysDefine.UI_TraUIScripts);
this.gameObject.transform.SetParent(_TraUIScripts, false);//让这个物体的脚本成为_TraUIScripts这个节点的子物体,方便管理
DontDestroyOnLoad(_TraCanvasTransform);//不销毁这个物体
_DicFormsPaths = ConfigManagerByJson.DicJson();//Json文件内容,存储到一个字典里
}
//初始化加载(根UI窗体)Canvas预设
private void InitRootCanvasLoading()
{
ResourceManager.Instace.Load(SysDefine.UI_Canvas);
}
都写了注释,看一遍就知道了,接下来才是重头戏,上面的代码不过是准备工作
第二步打开窗体方法
思维导图如下:
/// <summary>
/// 显示(打开)UI窗体
/// 1.加载与判断指定的UI窗体的名称,加载到“所有UI窗体”缓存集合中
/// 2.根据b不同的UI窗体的显示模式,分别作不同的加载处理
/// </summary>
/// <param name="uiFormName"></param>
public void ShowUIForms(string uiFormName)
{
BaseUIForm baseUIForm = null;//UI窗体基类
//参数的检查
if (string.IsNullOrEmpty(uiFormName)) return;
//根据UI窗体的名称,加载到"所有UI窗体"缓存集合中
baseUIForm = LoadFormToAllUIFormCatch(uiFormName);
if (baseUIForm == null) return;
if (baseUIForm.CurrenType.IsClearStack)
{
ClearStackArray();
}
//根据不同的UI窗体的显示模式,分别作不同的加载处理
switch (baseUIForm.CurrenType.UIForms_ShowMode)
{
case UIFormShowMode.Normal:
LoadUICurrentCache(uiFormName);
break;
case UIFormShowMode.ReverseChange:
PushUIFormToStack(uiFormName);
break;
case UIFormShowMode.HideOther:
EnterUIFormsAndHideOther(uiFormName);
break;
}
}
1.根据UI窗体的名称,加载到所有“UI窗体”缓存集合中
/// <summary>
/// 根据UI窗体的名称,加载到所有“UI窗体”缓存集合中
/// 功能:检查
/// </summary>
/// <param name="uiFormName"></param>
/// <returns></returns>
private BaseUIForm LoadFormToAllUIFormCatch(string uiFormName)
{
BaseUIForm baseUIResult = null;//加载的返回UI窗体
_DicALLUIForms.TryGetValue(uiFormName, out baseUIResult);
if (baseUIResult == null)
{
baseUIResult = LoadUIForm(uiFormName);
}
return baseUIResult;
}
思维导图如下:
2.不同窗体的显示模式
2.1-Normal
/// <summary>
/// 把当前窗体加载到"当前窗体"集合中
/// </summary>
/// <param name="uiFormName"></param>
private void LoadUICurrentCache(string uiFormName)
{
BaseUIForm baseUIForm;
BaseUIForm baseUIFormFromAllCache;
//如果"正在显示中"的集合中,存在整个UI窗体,则直接返回
_DicCurrentShowUIForms.TryGetValue(uiFormName, out baseUIForm);
if (baseUIForm != null) return;
//把当前窗体,加载到“正在显示"集合中"
_DicALLUIForms.TryGetValue(uiFormName, out baseUIFormFromAllCache);
if (baseUIFormFromAllCache != null)
{
_DicCurrentShowUIForms.Add(uiFormName, baseUIFormFromAllCache);
baseUIFormFromAllCache.Display();
}
}
2.2ReverseChange
//入栈
private void PushUIFormToStack(string uiFormName)
{
BaseUIForm baseUIForm;
if (_StaCurrentUIForms.Count > 0)
{
BaseUIForm topUIForm = _StaCurrentUIForms.Peek();
topUIForm.Freeze();
}
_DicALLUIForms.TryGetValue(uiFormName, out baseUIForm);
if (baseUIForm != null)
{
baseUIForm.Display();
_StaCurrentUIForms.Push(baseUIForm);
}
else
{
Debug.Log("baseUIForm==null,Please Check,uiFormName = " + uiFormName);
}
}
2.3 EnterUIFormsAndHideOther
/// <summary>
/// 打开窗体,且隐藏其他窗体
/// </summary>
/// <param name="strUIName"></param>
private void EnterUIFormsAndHideOther(string strUIName)
{
BaseUIForm baseUIForm;
BaseUIForm baseUIFormFromAll;
//参数检查
if (string.IsNullOrEmpty(strUIName)) return;
_DicCurrentShowUIForms.TryGetValue(strUIName, out baseUIForm);
if (baseUIForm == null) return;
//把正在显示集合和栈集合中所有的窗体都隐藏
foreach (BaseUIForm baseUIForms in _DicCurrentShowUIForms.Values)
{
baseUIForms.Hiding();
}
foreach (BaseUIForm baseUI in _StaCurrentUIForms)
{
baseUI.Hiding();
}
//把当前窗体加入到"正在显示窗体"集合中,而且做显示处理
_DicALLUIForms.TryGetValue(strUIName, out baseUIFormFromAll);
if (baseUIFormFromAll != null)
{
_DicCurrentShowUIForms.Add(strUIName, baseUIFormFromAll);
baseUIFormFromAll.Display();
}
}