Rewrited UI system

More simple and flexible
master
Quentin Franchi 3 years ago
parent 266be6a075
commit 4896cabdde

@ -0,0 +1,17 @@
using UnityEngine;
namespace LPQT.UI
{
public class ExampleUI : UIMenu
{
public override void Show()
{
Debug.Log(GetType().Name);
}
public override void Hide()
{
Debug.Log(GetType().Name);
}
}
}

@ -1,147 +1,147 @@
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using System.Reflection;
using System.Collections.Generic;
using System;
namespace LPQT.UI {
public class UIManager : MonoBehaviour {
//instance
public static UIManager INSTANCE { get; set; }
public UIMenu startMenu;
//events
public UnityEvent onMenuChanged;
//menus
private Component[] menus = new Component[0];
private UIMenu previousMenu;
public UIMenu PreviousMenu {
get {
return previousMenu;
}
}
using MOBA.Network;
private UIMenu currentMenu;
public UIMenu CurrentMenu {
get {
return currentMenu;
}
}
namespace LPQT.UI
{
public class UIManager : MonoBehaviour
{
public static UIManager Instance;
private void Awake(){
if(UIManager.INSTANCE) {
Destroy(gameObject);
return;
}
INSTANCE = this;
DontDestroyOnLoad(this);
[SerializeField] private UIMenu startMenu;
menus = GetComponentsInChildren<UIMenu>(true);
private UIMenu currentMenu = null;
private UIMenu previousMenu = null;
if(startMenu) {
ShowMenu(startMenu);
}
}
private UIMenu[] uiMenus = new UIMenu[0];
private bool initialized = false;
private void OnDestroy() {
if(UIManager.INSTANCE == this) {
INSTANCE = null;
private void Awake()
{
if(initialized)
{
return;
}
}
public void ShowMenu(UIMenu menu){
if(!menu) {
return;
if(Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
if (menus.Length == 0) {
Debug.LogError(menu.GetType() + " cannot be closed because menus is empty");
return;
}
uiMenus = GetComponentsInChildren<UIMenu>(true);
Show(startMenu);
ClearMenus();
initialized = true;
}
if(currentMenu) {
previousMenu = currentMenu;
currentMenu.Hide();
private void OnDestroy()
{
if(Instance != null)
{
Instance = null;
}
currentMenu = menu;
currentMenu.gameObject.SetActive(true);
menu.Show();
if(onMenuChanged != null) {
onMenuChanged.Invoke();
}
initialized = false;
}
/// <summary>
/// Show a menu by it's type
/// Show a UIMenu by it's type
/// </summary>
/// <typeparam name="T"></typeparam>
public void ShowMenu<T>() {
if (menus.Length == 0) {
Debug.LogError("Menus list empty");
return;
/// <returns></returns>
public static bool Show<T>()
{
if(Instance == null)
{
return false;
}
foreach (UIMenu menu in menus) {
if(menu is T) {
ShowMenu(menu);
return;
foreach (UIMenu uiMenu in Instance.uiMenus)
{
if(uiMenu is T)
{
Instance.Show(uiMenu);
return true;
}
}
return false;
}
/// <summary>
/// Get a menu by it's type
/// Get a UIMenu by it's type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetUIMenu<T>() {
foreach (UIMenu menu in menus) {
if(menu is T){
return (T) Convert.ChangeType(menu, typeof(T));
public static T Get<T>()
{
if(Instance == null)
{
return default(T);
}
foreach (MOBA.UI.UIMenu uiMenu in Instance.uiMenus)
{
if(uiMenu is T)
{
return (T) Convert.ChangeType(uiMenu, typeof(T));
}
}
return default(T);
}
/// <summary>
/// Hide a menu
/// Show and get UIMenu with it's object
/// </summary>
/// <param name="menu"></param>
public void HideMenu(UIMenu menu) {
if (menus.Length == 0) {
Debug.LogError(menu.GetType() + " cannot be closed because menus is empty");
return;
public static UIMenu ShowGet(UIMenu uiMenu)
{
if(Instance == null)
{
return null;
}
menu.gameObject.SetActive(false);
Instance.Show(uiMenu);
return Instance.uiMenus.FirstOrDefault(ui => ui.GetType() == uiMenu.GetType());
}
/// <summary>
/// Show the last menu
/// Display the wanted UIMenu
/// </summary>
public void ShowLastMenu() {
if(!previousMenu){
private void Show(UIMenu uiMenu)
{
if(!uiMenu)
{
return;
}
if (uiMenus.Length == 0)
{
Debug.LogError(uiMenu.GetType() + " not found.");
return;
}
ShowMenu(previousMenu);
Clear();
if(currentMenu)
{
currentMenu.gameObject.SetActive(false);
previousMenu = currentMenu;
}
currentMenu = uiMenu;
currentMenu.gameObject.SetActive(true);
}
/// <summary>
/// Hide all the menus
/// Clear all UIMenu
/// </summary>
public void ClearMenus(){
foreach (UIMenu menu in menus) {
menu.gameObject.SetActive(false);
private void Clear()
{
foreach (UIMenu uiMenu in uiMenus)
{
uiMenu.gameObject.SetActive(false);
}
}
}
}
}

@ -1,65 +1,27 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
namespace LPQT.UI {
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CanvasGroup))]
public abstract class UIMenu : MonoBehaviour {
public UnityEvent onMenuShow = new UnityEvent();
public UnityEvent onMenuHide = new UnityEvent();
private Animator animator;
private void Start(){
animator = GetComponent<Animator>();
using UnityEngine;
namespace LPQT.UI
{
public abstract class UIMenu : MonoBehaviour
{
private void OnEnable()
{
Show();
}
private void OnEnable(){
animator = GetComponent<Animator>();
private void OnDisable()
{
Hide();
}
/// <summary>
/// When the current menu is being shown
/// Called using OnEnable() when the menu is being showed
/// </summary>
public virtual void Show(){
if(onMenuShow != null){
onMenuShow.Invoke();
}
/*
if(animator) {
DoAnimation("show");
}
*/
}
protected virtual void Show() {}
/// <summary>
/// When the current menu is being hide
/// Called using OnDisable() when the menu is being hidden
/// </summary>
public virtual void Hide(){
if(onMenuHide != null){
onMenuHide.Invoke();
}
//DoAnimation("hide");
}
/// <summary>
/// Cancel action of the current menu
/// </summary>
public virtual void Cancel(){
Debug.Log("Cancel");
}
/*void DoAnimation(string animation) {
if(!animator){
return;
}
animator.SetTrigger(animation.ToLower());
}*/
protected virtual void Hide() {}
}
}
}

@ -1,13 +1,15 @@
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace LPQT.UI {
public static class UIUtils {
namespace LPQT.UI
{
public static class UIUtils
{
/// <summary>
/// Destroys the transform's childs.
/// </summary>
public static void CleanChilds(this Transform t){
public static void CleanChilds(this Transform t)
{
foreach (Transform c in t) {
Object.Destroy(c.gameObject);
}

Loading…
Cancel
Save