Menu.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /// Credit Adam Kapos (Nezz) - http://www.songarc.net
  2. /// Sourced from - https://github.com/YousicianGit/UnityMenuSystem
  3. /// Updated by SimonDarksideJ - Refactored to be a more generic component
  4. namespace UnityEngine.UI.Extensions
  5. {
  6. public abstract class Menu<T> : Menu where T : Menu<T>
  7. {
  8. public static T Instance { get; private set; }
  9. protected virtual void Awake()
  10. {
  11. Instance = (T)this;
  12. }
  13. protected virtual void OnDestroy()
  14. {
  15. Instance = null;
  16. }
  17. protected static void Open()
  18. {
  19. GameObject clonedGameObject = null;
  20. if (Instance == null)
  21. {
  22. MenuManager.Instance.CreateInstance(typeof(T).Name, out clonedGameObject);
  23. MenuManager.Instance.OpenMenu(clonedGameObject.GetMenu());
  24. }
  25. else
  26. {
  27. Instance.gameObject.SetActive(true);
  28. MenuManager.Instance.OpenMenu(Instance);
  29. }
  30. }
  31. protected static void Close()
  32. {
  33. if (Instance == null)
  34. {
  35. Debug.LogErrorFormat("Trying to close menu {0} but Instance is null", typeof(T));
  36. return;
  37. }
  38. MenuManager.Instance.CloseMenu(Instance);
  39. }
  40. public override void OnBackPressed()
  41. {
  42. Close();
  43. }
  44. }
  45. public abstract class Menu : MonoBehaviour
  46. {
  47. [Tooltip("Destroy the Game Object when menu is closed (reduces memory usage)")]
  48. public bool DestroyWhenClosed = true;
  49. [Tooltip("Disable menus that are under this one in the stack")]
  50. public bool DisableMenusUnderneath = true;
  51. public abstract void OnBackPressed();
  52. }
  53. }