UGUI / Unity
Unity UGUI Implementation (1): A UI Manager for Opening, Closing, Showing, and Hiding UI

Other articles in this series
- Unity UGUI Implementation (1): A UI Manager for Opening, Closing, Showing, and Hiding UI
- Unity UGUI Implementation (2): Scroll View implementation with dynamically created child elements
- Unity UGUI Implementation (3): Building in-game tabs with UI Toggle
- Unity UGUI Implementation (4): Displaying a 3D model in UI
- Unity UGUI Implementation (5): Building health bars and value bars with UI Slider
- Unity UGUI Implementation (6): Building in-game data sorting with UI Dropdown
Environment
- Windows 7
- Unity 5.2.4
Video tutorial
This tutorial is presented as a video. It was my first attempt at video-based teaching, so feedback was welcome. If this format worked, I planned to use it for the rest of the series.
Script
UIManager.cs
using UnityEngine;
using System.Collections.Generic;
public class UIManager : Singleton<UIManager>
{
private string UI_GAMEPANEL_ROOT = "Prefabs/GamePanel/";
public GameObject m_CanvasRoot;
public Dictionary<string, GameObject> m_PanelList = new Dictionary<string, GameObject>();
private bool CheckCanvasRootIsNull()
{
if (m_CanvasRoot == null)
{
Debug.LogError("m_CanvasRoot is Null, Please in your Canvas add UIRootHandler.cs");
return true;
}
else
{
return false;
}
}
private bool IsPanelLive(string name)
{
return m_PanelList.ContainsKey(name);
}
public GameObject ShowPanel(string name)
{
if (CheckCanvasRootIsNull())
return null;
if (IsPanelLive(name))
{
Debug.LogErrorFormat("[{0}] is Showing, if you want to show, please close first!!", name);
return null;
}
GameObject loadGo = Utility.AssetRelate.ResourcesLoadCheckNull<GameObject>(UI_GAMEPANEL_ROOT + name);
if (loadGo == null)
return null;
GameObject panel = Utility.GameObjectRelate.InstantiateGameObject(m_CanvasRoot, loadGo);
panel.name = name;
m_PanelList.Add(name, panel);
return panel;
}
public void TogglePanel(string name, bool isOn)
{
if (IsPanelLive(name))
{
if (m_PanelList[name] != null)
m_PanelList[name].SetActive(isOn);
}
else
{
Debug.LogErrorFormat("TogglePanel [{0}] not found.", name);
}
}
public void ClosePanel(string name)
{
if (IsPanelLive(name))
{
if (m_PanelList[name] != null)
Object.Destroy(m_PanelList[name]);
m_PanelList.Remove(name);
}
else
{
Debug.LogErrorFormat("ClosePanel [{0}] not found.", name);
}
}
public void CloseAllPanel()
{
foreach (KeyValuePair<string, GameObject> item in m_PanelList)
{
if (item.Value != null)
Object.Destroy(item.Value);
}
m_PanelList.Clear();
}
public Vector2 GetCanvasSize()
{
if (CheckCanvasRootIsNull())
return Vector2.one * -1;
RectTransform trans = m_CanvasRoot.transform as RectTransform;
return trans.sizeDelta;
}
}
UIRootHandler.cs
using UnityEngine;
public class UIRootHandler : MonoBehaviour {
void Awake () {
UIManager.Instance.m_CanvasRoot = gameObject;
}
}
References
- Unity Hardware Statistics
- GitHub - Utility
- Unity UGUI Principles (1): Canvas Rendering Mode
- Unity UGUI Principles (2): Canvas Scaler and UI Scaling
Attribution
Please credit ARKAI Studio and link back to this article when quoting or reposting.