diff --git a/Examples/MenuExample/MainScene.unity b/Examples/MenuExample/MainScene.unity index 021640a..94a88f4 100644 --- a/Examples/MenuExample/MainScene.unity +++ b/Examples/MenuExample/MainScene.unity @@ -187,6 +187,11 @@ Prefab: propertyPath: MenuScreens.Array.size value: 5 objectReference: {fileID: 0} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.size + value: 5 + objectReference: {fileID: 0} - target: {fileID: 4108131592329822, guid: f0f89c3fccbfa4bddbac45ae1effe466, type: 2} propertyPath: m_LocalPosition.x value: 0 @@ -254,6 +259,41 @@ Prefab: propertyPath: StartScreen value: 0 objectReference: {fileID: 0} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.data[0] + value: + objectReference: {fileID: 114026954515301400, guid: e9be8b59801ec4bbe829f57f7b2b9d88, + type: 2} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.data[1] + value: + objectReference: {fileID: 114359196589700260, guid: 5617592ee912047459b98264a09ddb8a, + type: 2} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.data[2] + value: + objectReference: {fileID: 114412859951304140, guid: 85d45cc7dbcc7490b91c0aa61f4e73c2, + type: 2} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.data[3] + value: + objectReference: {fileID: 114109697501719388, guid: 11104fd9ff8884a49b96b0d3d0accb5c, + type: 2} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: menuScreens.Array.data[4] + value: + objectReference: {fileID: 114030211315693384, guid: 5f7dc93bebf1e41e280baafd8eaa59ec, + type: 2} + - target: {fileID: 114785289659472098, guid: f0f89c3fccbfa4bddbac45ae1effe466, + type: 2} + propertyPath: startScreen + value: 0 + objectReference: {fileID: 0} m_RemovedComponents: [] m_ParentPrefab: {fileID: 100100000, guid: f0f89c3fccbfa4bddbac45ae1effe466, type: 2} m_IsPrefabParent: 0 diff --git a/Examples/MenuExample/Prefabs/Menus/MainMenu.prefab b/Examples/MenuExample/Prefabs/Menus/MainMenu.prefab index 250c76f..3123103 100644 --- a/Examples/MenuExample/Prefabs/Menus/MainMenu.prefab +++ b/Examples/MenuExample/Prefabs/Menus/MainMenu.prefab @@ -133,7 +133,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &114012036402784198 MonoBehaviour: m_ObjectHideFlags: 1 diff --git a/Scripts/MenuSystem/Menu.cs b/Scripts/MenuSystem/Menu.cs index f86a72c..4af4b2a 100644 --- a/Scripts/MenuSystem/Menu.cs +++ b/Scripts/MenuSystem/Menu.cs @@ -9,6 +9,7 @@ namespace UnityEngine.UI.Extensions { public static T Instance { get; private set; } + protected virtual void Awake() { Instance = (T)this; @@ -21,13 +22,17 @@ namespace UnityEngine.UI.Extensions protected static void Open() { + GameObject clonedGameObject = null; if (Instance == null) - MenuManager.Instance.CreateInstance(typeof(T).Name); - //MenuManager.Instance.CreateInstance(); + { + MenuManager.Instance.CreateInstance(typeof(T).Name, out clonedGameObject); + MenuManager.Instance.OpenMenu(clonedGameObject.GetMenu()); + } else + { Instance.gameObject.SetActive(true); - - MenuManager.Instance.OpenMenu(Instance); + MenuManager.Instance.OpenMenu(Instance); + } } protected static void Close() diff --git a/Scripts/MenuSystem/MenuManager.cs b/Scripts/MenuSystem/MenuManager.cs index d748bb6..fd63df1 100644 --- a/Scripts/MenuSystem/MenuManager.cs +++ b/Scripts/MenuSystem/MenuManager.cs @@ -1,6 +1,7 @@ /// 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; @@ -11,19 +12,35 @@ namespace UnityEngine.UI.Extensions [DisallowMultipleComponent] public class MenuManager : MonoBehaviour { - public Menu[] MenuScreens; - public int StartScreen = 0; + [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 menuStack = new Stack(); public static MenuManager Instance { get; set; } - private void Awake() + private void Start() { Instance = this; if (MenuScreens.Length > 0 + StartScreen) { - CreateInstance(MenuScreens[StartScreen].name); - OpenMenu(MenuScreens[StartScreen]); + var startMenu = CreateInstance(MenuScreens[StartScreen].name); + OpenMenu(startMenu.GetMenu()); } else { @@ -36,26 +53,26 @@ namespace UnityEngine.UI.Extensions Instance = null; } - public void CreateInstance() where T : Menu - { - var prefab = GetPrefab(); - - Instantiate(prefab, transform); - } - - public void CreateInstance(string MenuName) + public GameObject CreateInstance(string MenuName) { var prefab = GetPrefab(MenuName); - Instantiate(prefab, transform); + return Instantiate(prefab, transform); } - public void OpenMenu(Menu instance) + 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 (instance.DisableMenusUnderneath) + if (menuInstance.DisableMenusUnderneath) { foreach (var menu in menuStack) { @@ -66,12 +83,12 @@ namespace UnityEngine.UI.Extensions } } - var topCanvas = instance.GetComponent(); + var topCanvas = menuInstance.GetComponent(); var previousCanvas = menuStack.Peek().GetComponent(); topCanvas.sortingOrder = previousCanvas.sortingOrder + 1; } - menuStack.Push(instance); + menuStack.Push(menuInstance); } private GameObject GetPrefab(string PrefabName) @@ -86,23 +103,6 @@ namespace UnityEngine.UI.Extensions throw new MissingReferenceException("Prefab not found for " + PrefabName); } - private T GetPrefab() where T : Menu - { - // Get prefab dynamically, based on public fields set from Unity - // You can use private fields with SerializeField attribute too - var fields = this.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); - foreach (var field in fields) - { - var prefab = field.GetValue(this) as T; - if (prefab != null) - { - return prefab; - } - } - - throw new MissingReferenceException("Prefab not found for type " + typeof(T)); - } - public void CloseMenu(Menu menu) { if (menuStack.Count == 0) @@ -122,12 +122,12 @@ namespace UnityEngine.UI.Extensions public void CloseTopMenu() { - var instance = menuStack.Pop(); + var menuInstance = menuStack.Pop(); - if (instance.DestroyWhenClosed) - Destroy(instance.gameObject); + if (menuInstance.DestroyWhenClosed) + Destroy(menuInstance.gameObject); else - instance.gameObject.SetActive(false); + menuInstance.gameObject.SetActive(false); // Re-activate top menu // If a re-activated menu is an overlay we need to activate the menu under it @@ -150,4 +150,12 @@ namespace UnityEngine.UI.Extensions } } + public static class MenuExtensions + { + public static Menu GetMenu(this GameObject go) + { + return go.GetComponent(); + } + } + } \ No newline at end of file