private MainMenuButton _CreateMenu(string menuName) { Menu menu = Instantiate(m_menuPrefab, m_menuPanel.transform, false); menu.Items = null; m_menuButtonPrefab.gameObject.SetActive(false); MainMenuButton btn = Instantiate(m_menuButtonPrefab, m_topMenu.transform, false); btn.name = menuName; btn.Text = menuName; btn.Menu = menu; Text txt = btn.GetComponentInChildren <Text>(true); if (txt != null) { txt.text = menuName; } btn.gameObject.SetActive(true); return(btn); }
private void Awake() { List <Assembly> assemblies = new List <Assembly>(); foreach (string assemblyName in BHPath.RootAssemblies) { var asName = new AssemblyName(); asName.Name = assemblyName; Assembly asm = Assembly.Load(asName); if (asm == null) { Debug.LogWarning("Unable to load " + assemblyName); continue; } assemblies.Add(asm); } if (m_menuPanel == null) { m_menuPanel = gameObject; } if (m_topMenu == null) { m_topMenu = gameObject; } if (m_menuButtonPrefab == null) { Debug.LogError("Set Menu Button Prefab"); return; } if (m_menuPrefab == null) { Debug.LogError("Set Menu Prefab"); return; } bool wasButtonPrefabActive = m_menuButtonPrefab.gameObject.activeSelf; bool wasMenuPrefabActive = m_menuPrefab.gameObject.activeSelf; Dictionary <string, Menu> menuDictionary = new Dictionary <string, Menu>(); Dictionary <string, List <MenuItemInfo> > menuItemsDictionary = new Dictionary <string, List <MenuItemInfo> >(); Menu[] menus = m_menuPanel.GetComponentsInChildren <Menu>(true); for (int i = 0; i < menus.Length; ++i) { if (!menuDictionary.ContainsKey(menus[i].name)) { menuDictionary.Add(menus[i].name, menus[i]); if (menus[i].Items != null) { menuItemsDictionary.Add(menus[i].name, menus[i].Items.ToList()); } else { menuItemsDictionary.Add(menus[i].name, new List <MenuItemInfo>()); } } } Type[] menuDefinitions = assemblies.SelectMany(asm => asm.GetTypesWithAttribute(typeof(MenuDefinitionAttribute))).ToArray(); foreach (Type menuDef in menuDefinitions) { MethodInfo[] methods = menuDef.GetMethods(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < methods.Length; ++i) { MethodInfo mi = methods[i]; MenuCommandAttribute cmd = (MenuCommandAttribute)mi.GetCustomAttributes(typeof(MenuCommandAttribute), true).FirstOrDefault(); if (string.IsNullOrEmpty(cmd.Path)) { continue; } string[] pathParts = cmd.Path.Split('/'); if (pathParts.Length < 1) { continue; } string menuName = pathParts[0]; Menu menu; if (!menuDictionary.ContainsKey(menuName)) { m_menuButtonPrefab.gameObject.SetActive(false); m_menuPrefab.gameObject.SetActive(false); menu = Instantiate(m_menuPrefab, m_menuPanel.transform, false); menu.Items = null; MainMenuButton btn = Instantiate(m_menuButtonPrefab, m_topMenu.transform, false); btn.name = menuName; btn.Menu = menu; Text txt = btn.GetComponentInChildren <Text>(true); if (txt != null) { txt.text = menuName; } btn.gameObject.SetActive(true); menuDictionary.Add(menuName, menu); menuItemsDictionary.Add(menuName, new List <MenuItemInfo>()); } else { menu = menuDictionary[menuName]; } if (pathParts.Length == 1) { if (cmd.Hide) { menuItemsDictionary[menuName].Clear(); } } if (pathParts.Length < 2) { continue; } string path = string.Join("/", pathParts.Skip(1)); List <MenuItemInfo> menuItems = menuItemsDictionary[menuName]; MenuItemInfo menuItem = menuItems.Where(item => item.Path == path).FirstOrDefault(); if (menuItem == null) { menuItem = new MenuItemInfo(); menuItems.Add(menuItem); } menuItem.Path = string.Join("/", pathParts.Skip(1)); menuItem.Icon = !string.IsNullOrEmpty(cmd.IconPath) ? Resources.Load <Sprite>(cmd.IconPath) : null; menuItem.Text = pathParts.Last(); if (cmd.Validate) { Func <bool> validate = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), mi, false); if (validate == null) { Debug.LogWarning("method signature is invalid. bool Func() is expected. " + string.Join("/", pathParts)); } else { menuItem.Validate = new MenuItemValidationEvent(); menuItem.Validate.AddListener(new UnityAction <MenuItemValidationArgs>(args => args.IsValid = validate())); } } else { Action action = (Action)Delegate.CreateDelegate(typeof(Action), mi, false); if (action == null) { Debug.LogWarning("method signature is invalid. void Action() is expected. " + string.Join("/", pathParts)); } else { menuItem.Action = new MenuItemEvent(); menuItem.Action.AddListener(new UnityAction <string>(args => action())); } } if (cmd.Hide) { menuItems.Remove(menuItem); } } m_menuPrefab.gameObject.SetActive(wasMenuPrefabActive); m_menuButtonPrefab.gameObject.SetActive(wasButtonPrefabActive); } foreach (KeyValuePair <string, List <MenuItemInfo> > kvp in menuItemsDictionary) { menuDictionary[kvp.Key].SetMenuItems(kvp.Value.ToArray(), false); } }
private void Awake() { var asName = new AssemblyName(); asName.Name = "Assembly-CSharp"; Assembly csAssembly = Assembly.Load(asName); if (csAssembly == null) { Debug.LogWarning("Unable to load Assembly-CSharp"); return; } if (m_menuPanel == null) { m_menuPanel = gameObject; } if (m_topMenu == null) { m_topMenu = gameObject; } if (m_menuButtonPrefab == null) { Debug.LogError("Set Menu Button Prefab"); return; } if (m_menuPrefab == null) { Debug.LogError("Set Menu Prefab"); return; } bool wasButtonPrefabActive = m_menuButtonPrefab.gameObject.activeSelf; bool wasMenuPrefabActive = m_menuPrefab.gameObject.activeSelf; Dictionary <string, Menu> menuDictionary = new Dictionary <string, Menu>(); Dictionary <string, List <MenuItemInfo> > menuItemsDictionary = new Dictionary <string, List <MenuItemInfo> >(); Menu[] menus = m_menuPanel.GetComponentsInChildren <Menu>(true); for (int i = 0; i < menus.Length; ++i) { if (!menuDictionary.ContainsKey(menus[i].name)) { menuDictionary.Add(menus[i].name, menus[i]); if (menus[i].Items != null) { menuItemsDictionary.Add(menus[i].name, menus[i].Items.ToList()); } else { menuItemsDictionary.Add(menus[i].name, new List <MenuItemInfo>()); } } } Type[] menuDefinitions = csAssembly.GetTypesWithAttribute(typeof(MenuDefinitionAttribute)).ToArray(); SortedList <int, MainMenuButton> orderDictionary = new SortedList <int, MainMenuButton>(); //object[] attrs = menuDefinitions[0].GetTypeInfo().GetCustomAttributes(true); foreach (Type menuDef in menuDefinitions) { MenuDefinitionAttribute att = menuDef.GetTypeInfo().GetCustomAttributes(true)[0] as MenuDefinitionAttribute; int order = att.Order; MethodInfo[] methods = menuDef.GetMethods(BindingFlags.Static | BindingFlags.Public); for (int i = 0; i < methods.Length; ++i) { MethodInfo mi = methods[i]; MenuCommandAttribute cmd = (MenuCommandAttribute)mi.GetCustomAttributes(typeof(MenuCommandAttribute), true).FirstOrDefault(); if (string.IsNullOrEmpty(cmd.Path)) { continue; } string[] pathParts = cmd.Path.Split('/'); if (pathParts.Length < 2) { continue; } string menuName = pathParts[0]; Menu menu; if (!menuDictionary.ContainsKey(menuName)) { m_menuButtonPrefab.gameObject.SetActive(false); m_menuPrefab.gameObject.SetActive(false); menu = Instantiate(m_menuPrefab, m_menuPanel.transform, false); menu.Items = null; MainMenuButton btn = Instantiate(m_menuButtonPrefab, m_topMenu.transform, false); orderDictionary[order] = btn; btn.name = menuName; btn.Menu = menu; Text txt = btn.GetComponentInChildren <Text>(true); if (txt != null) { txt.text = menuName; } btn.gameObject.SetActive(true); menuDictionary.Add(menuName, menu); menuItemsDictionary.Add(menuName, new List <MenuItemInfo>()); } else { menu = menuDictionary[menuName]; } string path = string.Join("/", pathParts.Skip(1)); List <MenuItemInfo> menuItems = menuItemsDictionary[menuName]; MenuItemInfo menuItem = menuItems.Where(item => item.Path == path).FirstOrDefault(); if (menuItem == null) { menuItem = new MenuItemInfo(); menuItems.Add(menuItem); } menuItem.Path = string.Join("/", pathParts.Skip(1)); menuItem.Icon = !string.IsNullOrEmpty(cmd.IconPath) ? Resources.Load <Sprite>(cmd.IconPath) : null; menuItem.Text = pathParts.Last(); if (cmd.Validate) { Func <bool> validate = (Func <bool>)Delegate.CreateDelegate(typeof(Func <bool>), mi, false); if (validate == null) { Debug.LogWarning("method signature is invalid. bool Func() is expected. " + string.Join("/", pathParts)); } else { menuItem.Validate = new MenuItemValidationEvent(); menuItem.Validate.AddListener(new UnityAction <MenuItemValidationArgs>(args => args.IsValid = validate())); } } else { Action action = (Action)Delegate.CreateDelegate(typeof(Action), mi, false); if (action == null) { Debug.LogWarning("method signature is invalid. void Action() is expected. " + string.Join("/", pathParts)); } else { menuItem.Action = new MenuItemEvent(); menuItem.Action.AddListener(new UnityAction <string>(args => action())); } } if (cmd.Hide) { menuItems.Remove(menuItem); } } m_menuPrefab.gameObject.SetActive(wasMenuPrefabActive); m_menuButtonPrefab.gameObject.SetActive(wasButtonPrefabActive); } MainMenuButton prevButton = null; foreach (var entry in orderDictionary) { if (prevButton != null) { Vector2 location = prevButton.transform.position; float left = prevButton.GetComponent <RectTransform>().sizeDelta.x; location.x += left; entry.Value.transform.position = location; } float w = 0; Text txt = entry.Value.GetComponentInChildren <Text>(true); w = TextRenderer.MeasureText(txt.text, new System.Drawing.Font("Arial", txt.fontSize)).Width; RectTransform rect = entry.Value.GetComponent <RectTransform>(); rect.sizeDelta = new Vector2(w, rect.sizeDelta.y); prevButton = entry.Value; } foreach (KeyValuePair <string, List <MenuItemInfo> > kvp in menuItemsDictionary) { menuDictionary[kvp.Key].SetMenuItems(kvp.Value.ToArray(), false); } }