链接: https://pan.baidu.com/s/1pZCrySHQhBpQQ3g1C-WT-A 提取码: yyng

01:UI框架加强版_缓存


01:UI框架加强版_ide_02

01:UI框架加强版_缓存_03


01:定义常量

/****************************************************
文件:SysDine.cs
作者:唐孝辉
日期:2019/8/11 11:17:35
功能:常量配置
*****************************************************/

/// <summary>
/// UI窗体位置类型
/// </summary>
public enum UIFormsType
{
//普通窗体
Normal,
//固定窗体
Fixed,
//弹出窗体
PopUp,
}

/// <summary>
/// UI窗体显示类型
/// </summary>
public enum UIFormsShowMode
{
//普通
Normal,
//反向切换
ReverseChange,
//隐藏其他
HideOther,
}

/// <summary>
/// UI窗体透明度类型
/// </summary>
public enum UIFormsLucencyType
{
//完全透明,不能穿透
Lucency,
//半透明,不能穿透
TransLucece,
//低透明,不能穿透
ImPenetrable,
//可以穿透
Pentrate,
}


public class SysDefine
{

}

02

using UnityEngine;

//包裹3个类型
public class UIType
{
//UI窗体位置类型
public UIFormsType uiFormsType=UIFormsType.Normal;
// UI窗体显示类型
public UIFormsShowMode UiFormsShowMode = UIFormsShowMode.Normal;
//UI窗体透明度类型
public UIFormsLucencyType uiFormsLucencyType=UIFormsLucencyType.Lucency;
}

01:UI框架加强版_缓存_04


BaseForms

03定义四个状态

public class BaseUIForms : MonoBehaviour
{
//UI窗体类型
private UIType currentUiType = new UIType();

//当前窗体UI类型
public UIType CurrentUiType
{
get
{
return currentUiType;
}
set
{
currentUiType = value;
}
}


/// <summary>
/// 显示状态
/// </summary>
protected virtual void DisPlay()
{
this.gameObject.SetActive(true);
}

/// <summary>
/// 隐藏状态
/// </summary>
protected virtual void Hiding()
{
this.gameObject.SetActive(false);
}

/// <summary>
/// 重新显示状态
/// </summary>
protected virtual void ReDisPlay()
{
this.gameObject.SetActive(true);
}


/// <summary>
/// 冻结状态
/// </summary>
protected virtual void Freeze()
{
this.gameObject.SetActive(true);
}
}

04定义UI管理器

public class UIManager : MonoBehaviour
{
private static UIManager _instance;

public static UIManager instance
{
get
{
if (_instance==null)
{
_instance = GameObject.Find("GameManager").GetComponent<UIManager>();
}
return _instance;
}
}

//Ui窗体预设路径
private Dictionary<string, string> _DicFormsPaths;
//缓存所有UI窗体
private Dictionary<string, BaseUIForms> _DicALLUIForms;
//当前显示的UI窗口
private Dictionary<string, BaseUIForms> _DicCurrentShowUIForms;
//Ui根节点
private Transform _TraCanvasTransform = null;
//全屏幕显示的节点
private Transform _TraNormal = null;
//固定显示的节点
private Transform _TraFixed = null;
//弹出节点
private Transform _TraPopUp = null;
//Ui管理脚本的节点
private Transform _TraUIScripts = null;


private void Awake()
{
DontDestroyOnLoad(this);

}


}

01:UI框架加强版_屏幕显示_05

01:UI框架加强版_ide_06

public class UIManager : MonoBehaviour
{
private static UIManager _instance;

public static UIManager instance
{
get
{
if (_instance==null)
{
_instance = new GameObject("GameManager").AddComponent<UIManager>();
}
return _instance;
}
}

//Ui窗体预设路径
private Dictionary<string, string> _DicFormsPaths;
//缓存所有UI窗体
private Dictionary<string, BaseUIForms> _DicALLUIForms;
//当前显示的UI窗口
private Dictionary<string, BaseUIForms> _DicCurrentShowUIForms;
//Ui根节点
private Transform _TraCanvasTransform = null;
//全屏幕显示的节点
private Transform _TraNormal = null;
//固定显示的节点
private Transform _TraFixed = null;
//弹出节点
private Transform _TraPopUp = null;
//Ui管理脚本的节点
private Transform _TraUIScripts = null;


private void Awake()
{
_DicFormsPaths=new Dictionary<string, string>();
_DicALLUIForms=new Dictionary<string, BaseUIForms>();
_DicCurrentShowUIForms=new Dictionary<string, BaseUIForms>();
Init();
_DicFormsPaths.Add("LogOnUiForm", "UiPrefabs/LogOnUiForm"); //登录面板路径
}

private void Init()
{
GameObject canvas = ResourcesMgr.GetInstance().LoadAsset("Canvas", false);
DontDestroyOnLoad(canvas);
_TraCanvasTransform = canvas.transform;
_TraNormal = canvas.transform.Find("Normal").transform;
_TraFixed = canvas.transform.Find("Fixed").transform;
_TraPopUp = canvas.transform.Find("PopUp").transform;
_TraUIScripts= canvas.transform.Find("ScriptsMgr").transform;
this.transform.SetParent(canvas.transform);
}
}

01:UI框架加强版_缓存_07


01:UI框架加强版_缓存_08


01:UI框架加强版_ide_09


01:UI框架加强版_屏幕显示_10

public class UIManager : MonoBehaviour
{
private static UIManager _instance;

public static UIManager instance
{
get
{
if (_instance==null)
{
_instance = GameObject.Find("GameManager").GetComponent<UIManager>();
}
return _instance;
}
}

//Ui窗体预设路径
private Dictionary<string, string> _DicFormsPaths;
//缓存所有UI窗体
private Dictionary<string, BaseUIForms> _DicALLUIForms;
//当前显示的UI窗口
private Dictionary<string, BaseUIForms> _DicCurrentShowUIForms;
//Ui根节点
private Transform _TraCanvasTransform = null;
//全屏幕显示的节点
private Transform _TraNormal = null;
//固定显示的节点
private Transform _TraFixed = null;
//弹出节点
private Transform _TraPopUp = null;
//Ui管理脚本的节点
private Transform _TraUIScripts = null;


private void Awake()
{
_DicFormsPaths=new Dictionary<string, string>();
_DicALLUIForms=new Dictionary<string, BaseUIForms>();
_DicCurrentShowUIForms=new Dictionary<string, BaseUIForms>();
Init();
InitPaths();
}

private void Start()
{
ShowUIForms("LogOnUiForm");
}

private void InitPaths()
{
_DicFormsPaths.Add("LogOnUiForm", "UiPrefabs/LogOnUiForm"); //登录面板路径Dic
}

private void Init()
{
GameObject canvas = ResourcesMgr.GetInstance().LoadAsset("Canvas", false);
DontDestroyOnLoad(canvas);
_TraCanvasTransform = canvas.transform;
_TraNormal = canvas.transform.Find("Normal").transform;
_TraFixed = canvas.transform.Find("Fixed").transform;
_TraPopUp = canvas.transform.Find("PopUp").transform;
_TraUIScripts= canvas.transform.Find("ScriptsMgr").transform;
this.transform.SetParent(_TraUIScripts);
}

#region 打开窗口
public void ShowUIForms(string uiFormName)
{
BaseUIForms baseUiForms=null;
if (string.IsNullOrEmpty(uiFormName))
{
Debug.Log(uiFormName+"为空");
return;
}
baseUiForms= GetBaseUiForms(uiFormName);
if (baseUiForms==null)
{
Debug.Log("没找到");
return;
}
switch (baseUiForms.CurrentUiType.UiFormsShowMode)
{
case UIFormsShowMode.Normal:
//todo 当前窗体添加到当前窗体集合中
LoadUiToAllUiCaChe(uiFormName);
break;
case UIFormsShowMode.ReverseChange:
break;
case UIFormsShowMode.HideOther:
break;
}
}

//根据窗口名字 加载到ui窗口缓存集合中
private BaseUIForms GetBaseUiForms(string uiformName)
{
BaseUIForms baseUiForms = null;
_DicALLUIForms.TryGetValue(uiformName, out baseUiForms); //先看缓存里面有没有
if (baseUiForms==null)
{
baseUiForms = LoadBaseUiForms(uiformName);
}
return baseUiForms;
}

private BaseUIForms LoadBaseUiForms(string uiformName)
{
BaseUIForms baseUiForms=null;
GameObject gameObject = null;
string path = null;
_DicFormsPaths.TryGetValue(uiformName, out path);
if (path!=null)
{
gameObject = ResourcesMgr.GetInstance().LoadAsset(path, false);
}

if (gameObject!=null&&_TraCanvasTransform!=null)
{
baseUiForms = gameObject.GetComponent<BaseUIForms>();
if (baseUiForms==null)
{
Debug.Log("没有挂载脚本baseUiForms");
return null;
}
switch (baseUiForms.CurrentUiType.uiFormsType) //根据脚本的类型设置父节点
{
case UIFormsType.Normal:
gameObject.transform.SetParent(_TraNormal,false);
break;
case UIFormsType.Fixed:
gameObject.transform.SetParent(_TraFixed,false);
break;
case UIFormsType.PopUp:
gameObject.transform.SetParent(_TraPopUp,false);
break;
}
gameObject.SetActive(false);
_DicALLUIForms.Add(uiformName,baseUiForms); //添加缓存
return baseUiForms;
}
else
{
Debug.Log("路径不对"+uiformName+"没有找到对应的prefabs");
}
return null;
}

//把当前窗口缓存到当前窗口集合中
private void LoadUiToAllUiCaChe(string uiFormName)
{
BaseUIForms baseUiForms = null;
BaseUIForms baseCurrentUiForms = null;

_DicCurrentShowUIForms.TryGetValue(uiFormName, out baseUiForms);
if (baseUiForms != null)
{
//已经存在了
return;
}
if (_DicALLUIForms.TryGetValue(uiFormName, out baseCurrentUiForms))
{
//显示出来
baseCurrentUiForms.DisPlay();
_DicCurrentShowUIForms.Add(uiFormName, baseCurrentUiForms);
}
}
#endregion

}

01:UI框架加强版_ide_11


01:UI框架加强版_缓存_12


01:UI框架加强版_屏幕显示_13


05:定义栈集合

01:UI框架加强版_缓存_14

01:UI框架加强版_缓存_15


01:UI框架加强版_ide_16