mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 11:27:39 +02:00
Editor stuff
This commit is contained in:
@ -0,0 +1,65 @@
|
||||
/// Credit Adam Kapos (Nezz) - http://www.songarc.net
|
||||
/// Sourced from - https://github.com/YousicianGit/UnityMenuSystem
|
||||
/// Updated by SimonDarksideJ - Refactored to be a more generic component
|
||||
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
public abstract class Menu<T> : Menu where T : Menu<T>
|
||||
{
|
||||
public static T Instance { get; private set; }
|
||||
|
||||
|
||||
protected virtual void Awake()
|
||||
{
|
||||
Instance = (T)this;
|
||||
}
|
||||
|
||||
protected virtual void OnDestroy()
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
protected static void Open()
|
||||
{
|
||||
GameObject clonedGameObject = null;
|
||||
if (Instance == null)
|
||||
{
|
||||
MenuManager.Instance.CreateInstance(typeof(T).Name, out clonedGameObject);
|
||||
MenuManager.Instance.OpenMenu(clonedGameObject.GetMenu());
|
||||
}
|
||||
else
|
||||
{
|
||||
Instance.gameObject.SetActive(true);
|
||||
MenuManager.Instance.OpenMenu(Instance);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void Close()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Debug.LogErrorFormat("Trying to close menu {0} but Instance is null", typeof(T));
|
||||
return;
|
||||
}
|
||||
|
||||
MenuManager.Instance.CloseMenu(Instance);
|
||||
}
|
||||
|
||||
public override void OnBackPressed()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Menu : MonoBehaviour
|
||||
{
|
||||
[Tooltip("Destroy the Game Object when menu is closed (reduces memory usage)")]
|
||||
public bool DestroyWhenClosed = true;
|
||||
|
||||
[Tooltip("Disable menus that are under this one in the stack")]
|
||||
public bool DisableMenusUnderneath = true;
|
||||
|
||||
public abstract void OnBackPressed();
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9653e8a2f35df4e42b53f383913554af
|
||||
timeCreated: 1492685806
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,169 @@
|
||||
/// Credit Adam Kapos (Nezz) - http://www.songarc.net
|
||||
/// Sourced from - https://github.com/YousicianGit/UnityMenuSystem
|
||||
/// Updated by SimonDarksideJ - Refactored to be a more generic component
|
||||
/// Updated by SionDarksideJ - Fixed implementation as it assumed GO's we automatically assigned to instances
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
[AddComponentMenu("UI/Extensions/Menu Manager")]
|
||||
[DisallowMultipleComponent]
|
||||
public class MenuManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private Menu[] menuScreens;
|
||||
|
||||
public Menu[] MenuScreens
|
||||
{
|
||||
get { return menuScreens; }
|
||||
set { menuScreens = value; }
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private int startScreen = 0;
|
||||
|
||||
public int StartScreen
|
||||
{
|
||||
get { return startScreen; }
|
||||
set { startScreen = value; }
|
||||
}
|
||||
|
||||
private Stack<Menu> menuStack = new Stack<Menu>();
|
||||
|
||||
public static MenuManager Instance { get; set; }
|
||||
|
||||
private void Start()
|
||||
{
|
||||
Instance = this;
|
||||
if (MenuScreens.Length > 0 + StartScreen)
|
||||
{
|
||||
var startMenu = CreateInstance(MenuScreens[StartScreen].name);
|
||||
OpenMenu(startMenu.GetMenu());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Not enough Menu Screens configured");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
public GameObject CreateInstance(string MenuName)
|
||||
{
|
||||
var prefab = GetPrefab(MenuName);
|
||||
|
||||
return Instantiate(prefab, transform);
|
||||
}
|
||||
|
||||
public void CreateInstance(string MenuName, out GameObject menuInstance)
|
||||
{
|
||||
var prefab = GetPrefab(MenuName);
|
||||
|
||||
menuInstance = Instantiate(prefab, transform);
|
||||
}
|
||||
|
||||
public void OpenMenu(Menu menuInstance)
|
||||
{
|
||||
// De-activate top menu
|
||||
if (menuStack.Count > 0)
|
||||
{
|
||||
if (menuInstance.DisableMenusUnderneath)
|
||||
{
|
||||
foreach (var menu in menuStack)
|
||||
{
|
||||
menu.gameObject.SetActive(false);
|
||||
|
||||
if (menu.DisableMenusUnderneath)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Canvas topCanvas = menuInstance.GetComponent<Canvas>();
|
||||
if (topCanvas != null)
|
||||
{
|
||||
Canvas previousCanvas = menuStack.Peek().GetComponent<Canvas>();
|
||||
|
||||
if(previousCanvas != null)
|
||||
{
|
||||
topCanvas.sortingOrder = previousCanvas.sortingOrder + 1;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
menuStack.Push(menuInstance);
|
||||
}
|
||||
|
||||
private GameObject GetPrefab(string PrefabName)
|
||||
{
|
||||
for (int i = 0; i < MenuScreens.Length; i++)
|
||||
{
|
||||
if (MenuScreens[i].name == PrefabName)
|
||||
{
|
||||
return MenuScreens[i].gameObject;
|
||||
}
|
||||
}
|
||||
throw new MissingReferenceException("Prefab not found for " + PrefabName);
|
||||
}
|
||||
|
||||
public void CloseMenu(Menu menu)
|
||||
{
|
||||
if (menuStack.Count == 0)
|
||||
{
|
||||
Debug.LogErrorFormat(menu, "{0} cannot be closed because menu stack is empty", menu.GetType());
|
||||
return;
|
||||
}
|
||||
|
||||
if (menuStack.Peek() != menu)
|
||||
{
|
||||
Debug.LogErrorFormat(menu, "{0} cannot be closed because it is not on top of stack", menu.GetType());
|
||||
return;
|
||||
}
|
||||
|
||||
CloseTopMenu();
|
||||
}
|
||||
|
||||
public void CloseTopMenu()
|
||||
{
|
||||
var menuInstance = menuStack.Pop();
|
||||
|
||||
if (menuInstance.DestroyWhenClosed)
|
||||
Destroy(menuInstance.gameObject);
|
||||
else
|
||||
menuInstance.gameObject.SetActive(false);
|
||||
|
||||
// Re-activate top menu
|
||||
// If a re-activated menu is an overlay we need to activate the menu under it
|
||||
foreach (var menu in menuStack)
|
||||
{
|
||||
menu.gameObject.SetActive(true);
|
||||
|
||||
if (menu.DisableMenusUnderneath)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// On Android the back button is sent as Esc
|
||||
if (UIExtensionsInputManager.GetKeyDown(KeyCode.Escape) && menuStack.Count > 0)
|
||||
{
|
||||
menuStack.Peek().OnBackPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class MenuExtensions
|
||||
{
|
||||
public static Menu GetMenu(this GameObject go)
|
||||
{
|
||||
return go.GetComponent<Menu>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: adf0efbc9d5b148979111e50bec1893f
|
||||
timeCreated: 1492685806
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,22 @@
|
||||
/// Credit Adam Kapos (Nezz) - http://www.songarc.net
|
||||
/// Sourced from - https://github.com/YousicianGit/UnityMenuSystem
|
||||
/// Updated by SimonDarksideJ - Refactored to be a more generic component
|
||||
|
||||
namespace UnityEngine.UI.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// A base menu class that implements parameterless Show and Hide methods
|
||||
/// </summary>
|
||||
public abstract class SimpleMenu<T> : Menu<T> where T : SimpleMenu<T>
|
||||
{
|
||||
public static void Show()
|
||||
{
|
||||
Open();
|
||||
}
|
||||
|
||||
public static void Hide()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2fdf3dae987a8d44fa2e065bc4edaf3a
|
||||
timeCreated: 1501668792
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in New Issue
Block a user