创建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的更多相关文章

  1. Java中UIManager的几种外观的详细讲解

    Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...

  2. DuiLib——第一篇UIManager

    DUiLib 源码分析 --以UiLib 1.01版为分析目标 -------------------------------------------------------------------- ...

  3. intellij idea NoClassDefFoundError javax.swing.UIManager

    今天启动idea报 NoClassDefFoundError javax.swing.UIManager 可是明明配置好了java 环境 ,后来仔细想了一下只配置了java的bin目录在PATH里 随 ...

  4. Swing中使用UIManager批量自定义单一JComponent组件默认属性

    最近在研究Swing,被它的复杂性气的快吐血了,刚才本打算把JFrame的背景色换成白底,结果发现事情没想象中那么顺利,调用setBackground完全没有效果,猛然醒悟到JPanel本身是带不透明 ...

  5. UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类

    UIManage单实例: /// 单例模式的核心 /// 1,定义一个静态的对象 在外界访问 在内部构造 /// 2,构造方法私有化 private static UIManager _instanc ...

  6. Invariant Violation: requireNativeComponent: "RNCWKWebView" was not found in the UIManager.

    react-native  0.60以上版本安装第三方库的时候会autolink  出现这个问题是 我安装 react-native-webview 之后运行 ios出现的,这是因为ios 没有自动安 ...

  7. duilib库分析2.第一篇UIManager

    DUiLib 源码分析 ——以UiLib 1.01版为分析目标--------------------------------------------------------------------- ...

  8. Java中的UIManager简单实用(皮肤包)

    感谢大佬:https://blog.csdn.net/u010022051/article/details/52671860 注:具体详情请查阅Java API文档 /** * 设置图形界面外观 * ...

  9. UIManager的字体颜色参数对照表

    FileChooser.fileTypeHeaderTextTextArea.margincom.sun.java.swing.plaf.windows.WindowsSplitPaneUIcom.s ...

随机推荐

  1. net core体系-web应用程序-4asp.net core2.0 项目实战(1)-10项目各种全局帮助类

    本文目录 1.  前沿2.CacheHelper基于Microsoft.Extensions.Caching.Memory封装3.XmlHelper快速操作xml文档4.SerializationHe ...

  2. 虚拟机Oracle VM VirtualBox linux系统如何访问windows共享文件夹

    1. 在本机系统设置一个共享文件夹,用于与Ubuntu交互的区域空间.     2.右击状态栏上共享文件夹图标或菜单栏“设备-共享文件夹”,打开共享文件夹设置,如图示   3.点击共享文件夹设置框,右 ...

  3. 2018-2019-1 20189201 《LInux内核原理与分析》第八周作业

    只有在天足够黑的时候你才能看到星星. BY WAY GK 加油 一.书本第七章知识总结[可执行程序工作原理] 1. ELF目标文件格式 ELF全称Executable and Linkable For ...

  4. Gym 100963B

    Gym 100963B啊,郁闷,就tm调小了一点范围就A了,就写dp和贪心比较一下,范围到最大值的二倍-1就好了假设最大值的2倍以内能满足最优条件,当金额范围超过最大值2倍的时候:至于为什么,还不清楚 ...

  5. ISP PIPLINE (六) 3A 综述

    前言: 上一篇文章: ISP PIPLINE (五) Denoise 下一篇文章: (1)3A定义包括什么 Iris:自动光圈,根据环境自动调节光圈. 既然讲到光圈,就先看一下光圈是什么,以及它如何影 ...

  6. php+ajax文件上传

    php+ajax文件上传 html: <input id="user_real_name" class="input_show" type="t ...

  7. centos+git+gitolite 安装和部署

    一.部署环境 系统:CentOS 6.4x64 最小化安装 IP:192.168.52.131 git默认使用SSH协议,在服务器上基本上不用怎么配置就能直接使用.但是如果面向团队服务,需要控制权限的 ...

  8. .NET 文件格式相关开源项目

    在过去的2年里,我已确定把文件格式作为我的主要研究课题之一,NPOI就是在这样的研究课题下的产物.尽管从严格意义上讲NPOI只是POI的.NET版本,并不是我创造的,但是在开发过程中,我对OLE2有了 ...

  9. [LeetCode] Expressive Words 富于表现力的单词

    Sometimes people repeat letters to represent extra feeling, such as "hello" -> "he ...

  10. js上课笔记

    Html 结构化CSS 样式JavaScript 行为交互01.JavaScript基础02.JavaScript操作BOM对象03.JavaScript操作DOM对象 *****04.JavaScr ...