本文共 7870 字,大约阅读时间需要 26 分钟。
链接: https://pan.baidu.com/s/1pZCrySHQhBpQQ3g1C-WT-A 提取码: yyng
01:定义常量/**************************************************** 文件:SysDine.cs 作者:唐孝辉 邮箱: 1351105506@qq.com 日期:2019/8/11 11:17:35 功能:常量配置*****************************************************/////// UI窗体位置类型/// public enum UIFormsType{ //普通窗体 Normal, //固定窗体 Fixed, //弹出窗体 PopUp,}////// UI窗体显示类型/// public enum UIFormsShowMode{ //普通 Normal, //反向切换 ReverseChange, //隐藏其他 HideOther,}////// UI窗体透明度类型/// 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;}BaseForms 03定义四个状态
public class BaseUIForms : MonoBehaviour{ //UI窗体类型 private UIType currentUiType = new UIType(); //当前窗体UI类型 public UIType CurrentUiType { get { return currentUiType; } set { currentUiType = value; } } ////// 显示状态 /// protected virtual void DisPlay() { this.gameObject.SetActive(true); } ////// 隐藏状态 /// protected virtual void Hiding() { this.gameObject.SetActive(false); } ////// 重新显示状态 /// protected virtual void ReDisPlay() { this.gameObject.SetActive(true); } ////// 冻结状态 /// 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(); } return _instance; } } //Ui窗体预设路径 private Dictionary _DicFormsPaths; //缓存所有UI窗体 private Dictionary _DicALLUIForms; //当前显示的UI窗口 private Dictionary _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); }}
public class UIManager : MonoBehaviour{ private static UIManager _instance; public static UIManager instance { get { if (_instance==null) { _instance = new GameObject("GameManager").AddComponent(); } return _instance; } } //Ui窗体预设路径 private Dictionary _DicFormsPaths; //缓存所有UI窗体 private Dictionary _DicALLUIForms; //当前显示的UI窗口 private Dictionary _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 (); _DicALLUIForms=new Dictionary (); _DicCurrentShowUIForms=new Dictionary (); 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); }}
public class UIManager : MonoBehaviour{ private static UIManager _instance; public static UIManager instance { get { if (_instance==null) { _instance = GameObject.Find("GameManager").GetComponent05:定义栈集合(); } return _instance; } } //Ui窗体预设路径 private Dictionary _DicFormsPaths; //缓存所有UI窗体 private Dictionary _DicALLUIForms; //当前显示的UI窗口 private Dictionary _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 (); _DicALLUIForms=new Dictionary (); _DicCurrentShowUIForms=new Dictionary (); 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 (); 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}
转载地址:http://fzrxo.baihongyu.com/