UIManager
创建UIManager,管理所有UI面板
准备工作:
1. 创建Canvas并设置Tag为Main Canvas
2. 在Canvas下新建五个层级节点,因为UGUI显示层级越往下越靠前
using System;
using System.Collections.Generic;
using UnityEngine; public class UIManager : Singleton<UIManager>
{
public UIManager() { Init(); } private List<Transform> m_parent = new List<Transform>();
private List<UIBase> m_panel = new List<UIBase>(); private int current_ID; private void Init()
{
GameObject canvas = GameObject.FindGameObjectWithTag("Main Canvas"); if (canvas != null)
{
UnityEngine.UI.CanvasScaler scale = canvas.GetComponent<UnityEngine.UI.CanvasScaler>();
if (Screen.width / (float)Screen.height > / 9f)
{
scale.matchWidthOrHeight = ;
}
//获取节点
for (int i = ; i < (int)PanelType.Count; i++)
{
PanelType _panel = (PanelType)i;
m_parent.Add(canvas.transform.Find(_panel.ToString()));
}
}
else
{
Debug.LogError("Can't Find a GameObject whose Tag is 'Main Canvas'!");
}
} public void Open<T>(string panel_name, bool onlyOne = true, PanelType panel_type = PanelType.Window, PanelEvent panel_event = PanelEvent.None) where T : UIBase, new()
{
Transform parent = GetParent(panel_type); switch (panel_event)
{
case PanelEvent.Hide_Pre:
if (m_panel.Count != )
{
m_panel[m_panel.Count - ].SetActive(false);
}
break;
case PanelEvent.Close_Pre:
if (m_panel.Count != )
{
m_panel[m_panel.Count - ].Close();
m_panel.RemoveAt(m_panel.Count - );
}
break;
case PanelEvent.Close_All:
for (int i = ; i < m_panel.Count; i++)
{
m_panel[i].Close();
}
m_panel.Clear();
break;
default: break;
} try
{
UIBase panel = null; if (onlyOne)
{
for (int i = ; i < m_panel.Count; i++)
{
if (m_panel[i].panel_name == panel_name)
{
panel = m_panel[i];
m_panel.RemoveAt(i);
break;
}
} if (panel != null)
{
panel.SetParent(parent);
panel.First();
panel.Reopen();
}
else
{
panel = InitPanel<T>(panel_name, parent);
}
}
else
{
panel = InitPanel<T>(panel_name, parent);
} panel.panel_ID = NextID();
panel.panel_name = panel_name;
panel.panel_type = panel_type;
m_panel.Add(panel);
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
} public void Back()
{
if (m_panel.Count != )
{
m_panel[m_panel.Count - ].Close();
m_panel.RemoveAt(m_panel.Count - );
}
//刷新当前界面
if (m_panel.Count != )
{
m_panel[m_panel.Count - ].Reopen();
}
} public void Close(int panel_ID)
{
for (int i = ; i < m_panel.Count; i++)
{
if (m_panel[i].panel_ID == panel_ID)
{
m_panel[i].Close();
m_panel.RemoveAt(i);
break;
}
}
} public void Close(string panel_name)
{
for (int i = ; i < m_panel.Count; i++)
{
if (m_panel[i].panel_name == panel_name)
{
m_panel[i].Close();
m_panel.RemoveAt(i);
break;
}
}
} public void Close_All()
{
for (int i = ; i < m_panel.Count; i++)
{
m_panel[i].Close();
}
m_panel.Clear();
} public void Main()
{
for (int i = ; i < m_panel.Count; i++)
{
m_panel[i].Close();
}
m_panel.Clear(); Open<UI_Test>("UI_Main");
} public UIBase Top()
{
if (m_panel.Count > )
{
return m_panel[m_panel.Count - ];
}
else
{
return null;
}
} public UIBase Get(int panel_ID)
{
return m_panel.Find(x => x.panel_ID == panel_ID);
} public UIBase Get(string panel_name)
{
return m_panel.Find(x => x.panel_name == panel_name);
} private UIBase InitPanel<T>(string panel_name, Transform parent) where T : UIBase
{
UIBase component = null; //从对象池获取
GameObject go = PoolManager.Instance.Pop(panel_name); if (go == null)
{
//PanelData.Path[?]自定义数据存储预制体路径
string panel_path = PanelData.Path[panel_name];
GameObject prefab = Resources.Load<GameObject>(panel_path);
go = GameObject.Instantiate(prefab, parent);
}
else
{
go.transform.SetParent(parent, true);
} component = go.GetComponent<T>();
if (component == null)
{
component = go.AddComponent<T>();
} return component;
} private Transform GetParent(PanelType panel_type)
{
Transform parent = null; try
{
parent = m_parent[(int)panel_type];
}
catch (Exception e)
{
Debug.LogError(e.Message);
} return parent;
} private int NextID()
{
if (++current_ID >= int.MaxValue)
{
current_ID = ;
}
return current_ID;
}
} public enum PanelType
{
Bottom,
Base,
Window,
Widget,
Top,
Count,
} public enum PanelEvent
{
None,
Hide_Pre,
Close_Pre,
Close_All,
}
UIManager
UIBase是抽象类,需创建实例类继承
using System;
using System.Collections;
using UnityEngine; public abstract class UIBase : MonoBehaviour
{
[HideInInspector] public int panel_ID;
[HideInInspector] public string panel_name;
[HideInInspector] public PanelType panel_type; protected CanvasGroup panel_canvas; public virtual void Close(Action callBack = null)
{
if (callBack != null)
{
callBack();
}
PoolManager.Instance.Push(panel_name, gameObject);
} public virtual void Reopen()
{
Refresh();
SetActive(true);
} public virtual void Refresh()
{ } public void Delay(float time, Action callBack = null)
{
StartCoroutine(DelayedExecution(time, callBack));
} private IEnumerator DelayedExecution(float time, Action callBack)
{
yield return new WaitForSeconds(time); if (callBack != null)
{
callBack();
}
} public void First()
{
if (transform.parent != null)
{
transform.SetSiblingIndex(transform.parent.childCount - );
}
} public void SetName(string name)
{
transform.name = name;
} public void SetActive(bool active)
{
if (gameObject.activeSelf != active)
{
gameObject.SetActive(active);
}
} public void SetActive_Canvas(bool active)
{
if (panel_canvas != null)
{
panel_canvas.alpha = active ? : ;
panel_canvas.interactable = active;
panel_canvas.blocksRaycasts = active;
}
} public void SetParent(Transform parent)
{
if (transform.parent != parent)
{
transform.SetParent(parent);
}
} public override string ToString()
{
return panel_name;
}
}
UIBase
UIManager的更多相关文章
- Java中UIManager的几种外观的详细讲解
Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...
- DuiLib——第一篇UIManager
DUiLib 源码分析 --以UiLib 1.01版为分析目标 -------------------------------------------------------------------- ...
- intellij idea NoClassDefFoundError javax.swing.UIManager
今天启动idea报 NoClassDefFoundError javax.swing.UIManager 可是明明配置好了java 环境 ,后来仔细想了一下只配置了java的bin目录在PATH里 随 ...
- Swing中使用UIManager批量自定义单一JComponent组件默认属性
最近在研究Swing,被它的复杂性气的快吐血了,刚才本打算把JFrame的背景色换成白底,结果发现事情没想象中那么顺利,调用setBackground完全没有效果,猛然醒悟到JPanel本身是带不透明 ...
- UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类
UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...
- Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.
react-native 0.60以上版本安装第三方库的时候会autolink 出现这个问题是 我安装 react-native-webview 之后运行 ios出现的,这是因为ios 没有自动安 ...
- duilib库分析2.第一篇UIManager
DUiLib 源码分析 ——以UiLib 1.01版为分析目标--------------------------------------------------------------------- ...
- Java中的UIManager简单实用(皮肤包)
感谢大佬:https://blog.csdn.net/u010022051/article/details/52671860 注:具体详情请查阅Java API文档 /** * 设置图形界面外观 * ...
- UIManager的字体颜色参数对照表
FileChooser.fileTypeHeaderTextTextArea.margincom.sun.java.swing.plaf.windows.WindowsSplitPaneUIcom.s ...
随机推荐
- 手动部署 kubernetes HA 集群
前言 关于kubernetes HA集群部署的方式有很多种(这里的HA指的是master apiserver的高可用),比如通过keepalived vip漂移的方式.haproxy/nginx负载均 ...
- pycharm中join的应用
学习python这几天发现jion的两种用法 li = "alexericrain" v = ["_".join(li)] print (v) #第一种输出结果 ...
- pyqt pyside 窗口自动调整大小
pyqt pyside 窗口自动调整大小 在QTimer中一直调整 def initTimer(self): self.resizeTimer = QtCore.QTimer(self) self.r ...
- ETL测试教程
在我们了解ETL测试之前,先了解有关商业智能和数据仓库的重要性. 让我们开始吧 - 什么是BI? 商业智能是收集原始数据或业务数据并将其转化为有用和更有意义的信息的过程. 原始数据是一个组织每日事务的 ...
- 利用kibana插件对Elasticsearch进行批量操作
#############批量获取################# #获取所有数据 GET _mget { "docs": [ {"_index":" ...
- PHP 验证身份证
function isCreditNo($vStr) { $vCity = array( '11','12','13','14','15','21','22', '23','31','32','33' ...
- vue实现数据双向绑定的原理
一.知识准备Object.defineProperty( )方法可以直接在一个对象上定义一个新属性,或者修改一个已经存在的属性,并返回这个对象.Object.defineProperty(obj,pr ...
- STL复习之 map & vector --- disney HDU 2142
题目链接: https://vjudge.net/problem/40913/origin 大致题意: 这是一道纯模拟题,不多说了. 思路: map模拟,vector辅助 其中用了map的函数: er ...
- 使用第三方插件Gear Tacks 画齿轮
以下介绍第二种方法: 重复再生成一个大的齿轮 两个都保存起来: 再创建一个装配体环境. 接下来就是要达到使两个轮子配合转起来的效果! 步骤如下:
- Anaconda基础(一)
目录 Conda常见命令 环境管理 包管理 conda管理 小技巧 Conda常见命令 环境管理 创建环境 conda create -n 环境名 包列表 进入环境 source activate 环 ...