123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- using System.Collections.Generic;
- using System;
- using FairyGUI;
- using System.Linq;
- using ET;
- using UnityEngine;
- namespace GFGGame
- {
- public class ViewStructure
- {
- public string name;
- public object viewData;
- public IUIView iUIView;
- public bool backRefresh;
- public List<String> smallWindow;
- }
- /// <summary>
- /// 视图管理类
- /// 管理视图的显示、隐藏
- /// </summary>
- public class ViewManager
- {
- private static List<ViewStructure> _viewStack;
- private static Dictionary<string, IUIView> _viewDic;
- private static GComponent _bottomLayer;
- private static GComponent _topLayer;
- private static GComponent _guideLayer;
- private static GComponent _modalLayer;
- private static GComponent _alertLayer;
- private static GComponent _debugLayer;
- private static GComponent _floatLayer;
- private static bool _nowHideOthers = false; //正在关闭所有界面的循环中
- private static Dictionary<string, List<object[]>> _goBackDatas = new Dictionary<string, List<object[]>>();
-
- public static void Clear()
- {
- _viewStack.Clear();
- }
- public static void Init()
- {
- //设置CustomLoader
- UIObjectFactory.SetLoaderExtension(typeof(GFGGLoader));
- //通用资源,单独加,增加一次引用,不会被释放
- GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Common"));
- GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("CommonGame"));
- GFGUIPackage.AddPackage(ResPathUtil.GetUIPackagePath("Main"));
- //await GFGUIPackage.AddPackageAsync(ResPathUtil.GetUIPackagePath("Common"));
- UIConfig.buttonSound = (NAudioClip)UIPackage.GetItemAsset("Common", "click");
- //统一修改文字字体需要填写路径 ResIn/Font/FZKTJW--GB1-0
- UIConfig.defaultFont = "FZKTJW--GB1-0";
- //默认关闭点击窗口移至顶层的功能,不可打开,如哪个界面需要在界面中单独设置
- UIConfig.bringWindowToFrontOnClick = false;
- _viewDic = new Dictionary<string, IUIView>();
- _viewStack = new List<ViewStructure>();
- //初始化视图层容器
- _bottomLayer = CreateLayer("BottomLayer");
- //_bottomLayer.sortingOrder = ConstSortingOrder.Bottom;
- _topLayer = CreateLayer("TopLayer");
- _topLayer.sortingOrder = ConstViewLayerSortingOrder.TOP;
- _guideLayer = CreateLayer("GuideLayer");
- _guideLayer.sortingOrder = ConstViewLayerSortingOrder.Guide;
- _modalLayer = CreateLayer("ModalLayer");
- _modalLayer.sortingOrder = ConstViewLayerSortingOrder.Modal;
- _alertLayer = CreateLayer("AlertLayer");
- _alertLayer.sortingOrder = ConstViewLayerSortingOrder.Alert;
- //debug层
- _debugLayer = CreateLayer("DebugLayer");
- _debugLayer.sortingOrder = ConstViewLayerSortingOrder.Debug;
- _floatLayer = CreateLayer("FloatLayer");
- _floatLayer.sortingOrder = ConstViewLayerSortingOrder.Float;
- SetMaskAlpha(0.6f);
- }
- public static void AddChildToBottomLayer(GObject gObject)
- {
- _bottomLayer.AddChild(gObject);
- }
- public static void AddChildToTopLayer(GObject gObject)
- {
- _topLayer.AddChild(gObject);
- }
- public static void AddChildToGuideLayer(GObject gObject)
- {
- _guideLayer.AddChild(gObject);
- }
- public static void AddChildToModalLayer(GObject gObject)
- {
- _modalLayer.AddChild(gObject);
- }
- public static void AddChildToAlertLayer(GObject gObject)
- {
- _alertLayer.AddChild(gObject);
- }
- public static void AddChildToDebugLayer(GObject gObject)
- {
- _debugLayer.AddChild(gObject);
- }
- public static void AddChildToFloatLayer(GObject gObject)
- {
- _floatLayer.AddChild(gObject);
- }
- public static float ViewWidth
- {
- get
- {
- //这里做了最大宽度适配
- float maxAspectRatio = 1080 * 1.0f / 1920;
- if (Screen.width * 1.0f / Screen.height > maxAspectRatio)
- {
- return GRoot.inst.height * maxAspectRatio;
- }
- return GRoot.inst.width;
- }
-
- }
- /// <summary>
- /// 显示一个视图
- /// </summary>
- /// <param name="viewName">要显示的视图名称</param>
- /// <param name="viewData">要传递给视图的参数</param>
- /// <param name="goBackParams">从该视图返回的视图信息</param>
- /// <param name="hideOthers">是否关闭其他视图</param>
- /// <param name="backRefresh">返回上一个界面的时候,上一个界面是否需要刷新界面</param>
- public static bool Show(string fullViewName, object viewData = null, bool hideOthers = false, bool backRefresh = true,bool isHideToShow = false)
- {
- string name = GetName(fullViewName);
- if (!GameGlobal.skipCheckOpen && !FunctionOpenDataManager.Instance.CheckIsFunOpenById(name))
- {
- return false;
- }
- if (hideOthers)
- {
- HideAllView(name);
- }
- IUIView obj = null;
- if (_viewDic.ContainsKey(name))
- {
- obj = _viewDic[name];
- }
- else
- {
- obj = CreateViewInstance(fullViewName) as IUIView;
- obj.viewName = name;
- _viewDic.Add(name, obj);
- }
- if (obj != null)
- {
- IUIView view = (IUIView)obj;
- view.viewData = viewData;
- if (!view.isShowing)
- {
- if (isHideToShow && _viewStack.Count > 0){
- view.backRefresh = _viewStack[_viewStack.Count - 1].backRefresh;
- _viewStack[_viewStack.Count - 1].iUIView = obj;
- }
- else
- view.backRefresh = true;
- view.Show();
- }
- else
- {
- view.Refresh();
- }
- LogUtil.LogDev("当前打开:" + name);
- }
- if (name == "MainUIView")
- {
- _viewStack.Clear();
- }
- //判断是否需要保存界面数据, 会帮助关闭上一个保存界面
- if (obj.isReturnView && (_viewStack.Count <= 0 || (_viewStack.Count > 0 && _viewStack[_viewStack.Count - 1].name != name)))
- {
- //保存上一个界面是否需要返回刷新
- if (!isHideToShow && _viewStack.Count > 1)
- _viewStack[_viewStack.Count - 1].backRefresh = backRefresh;
-
- ViewStructure viewStructure = new ViewStructure();
- viewStructure.name = name;
- viewStructure.viewData = viewData;
- viewStructure.iUIView = obj;
- _viewStack.Add(viewStructure);
- if (_viewStack.Count > 1 && !hideOthers) {
- if (_viewStack[_viewStack.Count - 2].smallWindow == null)
- _viewStack[_viewStack.Count - 2].smallWindow = new List<string>();
- _viewStack[_viewStack.Count - 2].smallWindow.Clear();
- //把开着的小弹窗存起来
- foreach (var objName in _viewDic.Keys)
- {
- IUIView view = (IUIView)_viewDic[objName];
- if (view.isReturnWindow)
- {
- if (view.isShowing)
- {
- _viewStack[_viewStack.Count - 2].smallWindow.Add(objName);
- view.Hide();
- }
- }
- }
- _viewStack[_viewStack.Count - 2].iUIView.Hide();
- }
- }
- return true;
- }
- //
- /// <summary>
- /// 界面可返回栈里的跳转
- /// </summary>
- /// <param name="viewName">跳转到界面缓存栈里的某个界面,使用这个参数时写在打开下一个界面后</param>
- /// <param name="count">删除队列中的倒数几个,较灵活应对更多种情况,可自由控制</param>
- public static void DeleteViewStackCountDown(string viewName, int count = 0)
- {
- if (viewName != null && viewName != "")
- {
- for (int i = _viewStack.Count - 2; i > 0; i--)
- {
- ViewStructure viewStructure = _viewStack[i];
- if (viewStructure.name == viewName)
- break;
- _viewStack.RemoveAt(i);
- }
- return;
- }
- for (int i = 0; i < count; i++) {
- if (_viewStack.Count <= 1)
- break;
- _viewStack.RemoveAt(_viewStack.Count-1);
- }
- }
- public static bool isViewOpen(string fullViewName)
- {
- string name = GetName(fullViewName);
- IUIView obj = null;
- if (_viewDic.ContainsKey(name))
- {
- obj = _viewDic[name];
- if (obj != null)
- {
- IUIView view = (IUIView)obj;
- if (view.isShowing) return true;
- }
- }
- return false;
- }
- public static bool Show<T>(object viewData = null, bool hideOthers = false, bool backRefresh = true) where T : class, new()
- {
- // string[] names = typeof(T).FullName.Split('.');
- // string viewName = names[names.Length - 1];
- //string name = GetName(typeof(T).FullName);
- return ViewManager.Show(typeof(T).FullName, viewData, hideOthers, backRefresh);
- }
- public static void HideWin(string viewName)
- {
- if (_nowHideOthers)
- return;
-
- if (_viewStack.Count >= 1)
- {
- bool hasShowingView = false;
- bool needShowNextView = false;
- bool backRefresh = true;
- foreach (var info in _viewDic.Keys)
- {
- IUIView objIsShowing = _viewDic[info];
- if (objIsShowing != null && objIsShowing.isShowing)
- {
- hasShowingView = true;
- break;
- }
- }
- ViewStructure viewStructure = _viewStack[_viewStack.Count - 1];
- if (_viewStack.Count == 1)
- {
- //没有界面显示了,栈被清除剩1个的时候,做保底
- if (!hasShowingView)
- needShowNextView = true;
- }
- else {
- if (!hasShowingView || (viewStructure.iUIView.isReturnView && viewStructure.name == viewName))
- {
- //关闭自己,在队列里去除
- if (viewStructure.iUIView.isReturnView && viewStructure.name == viewName) {
- backRefresh = viewStructure.backRefresh;
- _viewStack.RemoveAt(_viewStack.Count - 1);
- }
- if (_viewStack.Count >= 1)
- needShowNextView = true;
- }
- }
- if (needShowNextView) {
- viewStructure = _viewStack[_viewStack.Count - 1];
- ViewManager.Show($"GFGGame.{viewStructure.name}", viewStructure.viewData, false, backRefresh, true);
- //重新打开小弹窗
- foreach (var objName in viewStructure.smallWindow)
- {
- ViewManager.Show($"GFGGame.{objName}");
- }
- //foreach (var objName in _viewDic.Keys)
- //{
- // if (objName != viewStructure.name)
- // {
- // IUIView view = (IUIView)_viewDic[objName];
- // if (view.isShowing)
- // view.Show();
- // }
- //}
- }
- }
- }
- public static void Hide(string fullViewName)
- {
- string name = GetName(fullViewName);
- if (!_viewDic.ContainsKey(name))
- {
- return;
- }
- object obj = _viewDic[name];
- if (obj != null)
- {
- IUIView view = (IUIView)obj;
- view.Hide();
- LogUtil.LogDev("当前关闭:" + name);
- }
- }
- public static void Hide<T>()
- {
- //string name = GetName(typeof(T).FullName);
- Hide(typeof(T).FullName);
- }
- public static void GoBackFrom(string fullViewName, bool hideOther = true)
- {
- string name = GetName(fullViewName);
- ViewManager.Hide(name);
- foreach (var info in _viewDic.Keys)
- {
- IUIView objIsShowing = _viewDic[info];
- if (objIsShowing != null && objIsShowing.isShowing)
- {
- return;
- }
- }
- MainDataManager.Instance.ViewType = 0;
- ViewManager.Show<MainUIView>(null, true);
- }
- public static object[] GetGoBackDatas(string fullViewName)
- {
- //string name = GetName(fullViewName);
- object[] value = null;
- //if (_goBackDatas.ContainsKey(name) && _goBackDatas[name].Count > 0)
- //{
- // value = _goBackDatas[name][_goBackDatas[name].Count - 1];
- //}
- return value;
- }
- public static IUIView GetUIView(string viewName)
- {
- if (_viewDic.ContainsKey(viewName))
- {
- IUIView obj = _viewDic[viewName];
- if (obj != null && obj.isShowing)
- {
- return obj as IUIView;
- }
- }
- return null;
- }
- public static void ClearUIView(string viewName)
- {
- if (!string.IsNullOrEmpty(viewName) && _viewDic.ContainsKey(viewName))
- {
- if (_viewDic[viewName] != null && !_viewDic[viewName].isShowing)
- {
- // _viewDic[viewName] = null;
- _viewDic.Remove(viewName);
- }
- }
- }
- public static void HideAllView(string excludeViewName = null)
- {
- for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
- {
- int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
- KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
- if (kv.Key != excludeViewName)
- {
- if (kv.Key == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
- _nowHideOthers = true;
- Hide(kv.Key);
- }
- }
- _nowHideOthers = false;
- // _viewDic.Clear();
- // foreach (string viewName in _viewDic.Keys)
- // {
- // if (viewName != excludeViewName)
- // {
- // if (viewName == typeof(FunctionOpenView).Name) continue;//功能开启界面不能强制关闭
- // Hide(viewName);
- // }
- // }
- }
- public static void CheckDispose()
- {
- for (int i = _viewDic.Keys.Count - 1; i >= 0; i--)//不用foreach是因为:循环过程中可能会触发dispose,导致_viewDic.Keys变化,最终报错
- {
- int index = i > _viewDic.Keys.Count - 1 ? _viewDic.Keys.Count - 1 : i;//直接去最后一个,不用i是因为关闭一个界面可能会连带关闭其他界面,最终i比_viewDic.Keys.Count大而报错
- KeyValuePair<string, IUIView> kv = _viewDic.ElementAt(index);
- if (kv.Value.isShowing == true) continue;
- // if (kv.Value.packageName == ResPathUtil.GetUIPackagePath("CommonGame") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Common") || kv.Value.packageName == ResPathUtil.GetUIPackagePath("Main")) return;//这几个包不释放
- if(_viewDic.Keys.Count <= 10) return; //打开界面小于10个就不销毁了
- long currentTime = TimeHelper.ClientNowSeconds();
- long closeTime = kv.Value.closeTime;
- if (closeTime > 0 && currentTime - closeTime >= TimeUtil.SECOND_PER_MUNITE * 1)
- {
- kv.Value.closeTime = 0;
- kv.Value.Dispose();
- }
- }
- }
- private static object CreateViewInstance(string name)
- {
- //LogUtil.LogFormatDev("CreateViewInstance {0}", name);
- Type type = Type.GetType(name);
- if (type != null)
- {
- return Activator.CreateInstance(type);
- }
- return null;
- }
- private static GComponent CreateLayer(string name)
- {
- GComponent layer = new GComponent();
- layer.name = name;
- GRoot.inst.AddChild(layer);
- layer.SetSize(GRoot.inst.size.x, GRoot.inst.size.y);
- layer.AddRelation(GRoot.inst, RelationType.Size);
- return layer;
- }
- public static bool CheckIsTopView(GComponent viewCom)
- {
- if (ViewManager.isViewOpen(typeof(GuideView).Name)) return false;
- if (viewCom.parent != null)
- {
- int index = viewCom.parent.GetChildIndex(viewCom);
- if (index == viewCom.parent.numChildren - 1 && GRoot.inst.GetTopWindow() == null)
- {
- return true;
- }
- }
- if (GRoot.inst.GetTopWindow() == viewCom)
- {
- return true;
- }
- return false;
- }
- public static string GetName(string fullName)
- {
- string[] names = fullName.Split('.');
- string name = names[names.Length - 1];
- return name;
- }
- public static void SetMaskAlpha(float alpha)
- {
- GRoot.inst.modalLayer.alpha = alpha;
- }
- /// <summary>
- /// 任务界面跳转
- /// </summary>
- /// <param name="jumpId"></param>
- public static void JumpToView(string jumpId, object[] param, bool hideOther = false, bool backRefresh = true, Action onSuccess = null)
- {
- switch (jumpId)
- {
- case nameof(LeagueAnswerView):
- if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
- {
- ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
- ViewManager.Show($"GFGGame.{jumpId}");
- }
- else
- {
- if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
- ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
- }
- break;
- case nameof(LeagueView):
- if (!FunctionOpenDataManager.Instance.CheckIsFunOpenById(nameof(LeagueView))) return;
- if (LeagueDataManager.Instance.Type == LeagueJoinType.Join)
- {
- ViewManager.Show<LeagueView>(null, hideOther, backRefresh);
- }
- else
- {
- ViewManager.Show<LeagueJoinView>(null, hideOther, backRefresh);
- }
- break;
- case nameof(StoreView):
- ViewManager.Show<StoreView>(param, hideOther, backRefresh);
- break;
- case nameof(StoryChapterListView):
- ViewManager.Show($"GFGGame.{jumpId}", param, hideOther, backRefresh);
- break;
- case nameof(StoryChapterView):
- ViewManager.Show<StoryChapterView>(param[0], hideOther, backRefresh);
- break;
- case nameof(FirstChargeBonusView):
- ViewManager.Show<FirstChargeBonusView>(param, false, backRefresh);
- break;
- case nameof(ClothingSyntheticView):
- ViewManager.Show<ClothingSyntheticView>(param, hideOther, backRefresh);
- break;
- case nameof(LuckyBoxView):
- if(param.Length > 0)
- ViewManager.Show<LuckyBoxView>(param[0], hideOther, backRefresh);
- else
- ViewManager.Show<LuckyBoxView>(null, hideOther, backRefresh);
- break;
- default:
- ViewManager.Show($"GFGGame.{jumpId}", null, hideOther, backRefresh);
- break;
- }
- onSuccess?.Invoke();
- }
- }
- }
|