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 ...
随机推荐
- java 安装以及配置
1.下载 JDK 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 2.环境变量配 ...
- nginx 错误502 upstream sent too big header while reading response header from upstream
查看nginx的错误日志,得到以下错误信息:upstream sent too big header while reading response header from upstream按字面意思理 ...
- 【Spring Boot】关于上传文件例子的剖析
目录 Spring Boot 上传文件 功能实现 增加ControllerFileUploadController 增加ServiceStorageService 增加一个Thymeleaf页面 修改 ...
- Jarvis OJ 一些简单的re刷题记录和脚本
[61dctf] androideasy 164求解器 50 相反 脚本如下: s='' a=113, 123, 118, 112, 108, 94, 99, 72, 38, 68, 72, 87, ...
- 15,EasyNetQ-高级API
EasyNetQ的使命是为基于RabbitMQ的消息传递提供最简单的API. 核心IBus接口有意避免公开AMQP概念,如交换,绑定和队列,而是实现基于消息类型的默认交换绑定队列拓扑. 对于某些场景, ...
- python 将list的值赋值给变量
list中有n个值,赋值给k个变量:a1, a2, ……,ak n = k时:a1, a2, ……,ak = list n > k时:使用1式报错:ValueError: too many va ...
- XVII Open Cup named after E.V. Pankratiev. GP of Moscow Workshops
A. Centroid Tree 枚举至多两个重心作为根,检查对于每个点是否都满足$2size[x]\leq size[father[x]]$即可. #include<stdio.h> # ...
- 解决sql_mode=only_full_group_by的问题
1.mysql查询报错: ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_ ...
- Linux 上安装JDK
JDK下载 下载 JDK Linux 版本(注意看自己安装 Linux 系统的位数,本人的是64位) 1.oracle 官网下载地址:jdk-7u80-linux-x64.gz(可能需要注册下载,嫌麻 ...
- JTS相关资料和示例
示例 JTS基本概念和使用 JTS Geometry之间的关系 JTS algorithm package