/// <summary> /// Remove menu by destroying all its coressponding RectTransform`s. /// Support Undo feature. /// </summary> /// <param name="index">Index of menu to destroy</param> public void RemoveMenu(int index) { Undo.SetCurrentGroupName("Remove Menu"); int group = Undo.GetCurrentGroup(); Undo.RegisterCompleteObjectUndo(swipyMenu, "RemoveMenu"); var menusList = new List <SwipyMenu.Menu>(swipyMenu.menus); Undo.DestroyObjectImmediate(menusList[index].menu.gameObject); Undo.DestroyObjectImmediate(menusList[index].header.gameObject); menusList.RemoveAt(index); swipyMenu.menus = menusList.ToArray(); if (swipyMenu.defaultMenuIndex == index && swipyMenu.menus.Length > 0) { var newIndex = index - 1; if (newIndex < 0) { newIndex = 0; } firstToShowOnLoadMenu = swipyMenu.menus[newIndex]; swipyMenu.defaultMenuIndex = newIndex; } ExpandHeaders(); Undo.CollapseUndoOperations(group); }
/// <summary> /// Create all nonexistent required components. /// </summary> public void Initialize() { for (int i = 0; i < transform.childCount; i++) { if (transform.GetChild(i).name == "Headers") { headersParent = transform.GetChild(i).GetComponent <RectTransform>(); } if (transform.GetChild(i).name == "Menus") { menusParent = transform.GetChild(i).GetComponent <RectTransform>(); if (menusParent.GetChild(0).name == "Content") { menusContent = menusParent.GetChild(0).GetComponent <RectTransform>(); } } } if (menusParent != null) { swipyMenu = menusParent.GetComponent <SwipyMenu>(); var scrollRect = menusParent.GetComponent <SwipyMenuScrollRect>(); swipyMenuScrollRect = scrollRect; swipyMenu.InitializeEditor(menusScrollRect: scrollRect, menusRect: menusParent); if (swipyMenu.menus.Length > 0) { firstToShowOnLoadMenu = swipyMenu.menus[swipyMenu.defaultMenuIndex]; } swipyMenu.menusMask = menusParent.GetComponent <RectMask2D>(); } else { CreateMenusBase(); } if (headersParent != null) { swipyMenu.InitializeEditor(headersRect: headersParent); } else { CreateHeadersBase(); } swipyMenu.headersMask = GetComponent <RectMask2D>(); DriveRectTransforms(); initialized = true; }
/// <summary> /// Create each menu RectTransform and all necessary components. /// </summary> public void CreateMenu() { Undo.SetCurrentGroupName("Create Menu"); int group = Undo.GetCurrentGroup(); Undo.RecordObject(swipyMenu, "Create Menu"); int index = swipyMenu.menus.Length; //create header var currentHeaderName = "Header " + (index + 1); var currentHeader = CreateGOWithRectTransform(currentHeaderName, headersParent); Undo.RegisterCreatedObjectUndo(currentHeader.gameObject, "Header"); currentHeader.gameObject.AddComponent <CanvasGroup>(); var swipyMenuHeader = currentHeader.gameObject.AddComponent <SwipyMenuHeader>(); var buttonRect = CreateGOWithRectTransform("Button", currentHeader, offsetMin: new Vector2(2f, 2f), offsetMax: new Vector2(-2f, -2f)); buttonRect.gameObject.AddComponent <Image>().color = new Color32(255, 255, 255, 80); var button = buttonRect.gameObject.AddComponent <Button>(); var colors = button.colors; colors.normalColor = new Color32(255, 255, 255, 20); colors.highlightedColor = new Color32(255, 255, 255, 80); colors.pressedColor = new Color32(255, 255, 255, 255); colors.disabledColor = new Color32(128, 128, 128, 20); button.colors = colors; UnityEventTools.AddIntPersistentListener(button.onClick, swipyMenu.HeaderClickHandler, index + 1); swipyMenuHeader.button = button; button.navigation = new Navigation() { mode = Navigation.Mode.None }; var text = CreateGOWithRectTransform("Text", currentHeader).gameObject.AddComponent <Text>(); text.raycastTarget = false; text.text = currentHeaderName; text.fontSize = 14; text.alignment = TextAnchor.MiddleCenter; swipyMenuHeader.text = text; // create menu var contentRect = menusParent.GetChild(0).GetComponent <RectTransform>(); var currentMenu = CreateGOWithRectTransform("Menu" + (index + 1), contentRect); Undo.RegisterCreatedObjectUndo(currentMenu.gameObject, "Menu"); var bgImage = CreateGOWithRectTransform("Bg", currentMenu, offsetMin: new Vector2(5f, 5f), offsetMax: new Vector2(-5f, -5f)).gameObject.AddComponent <Image>(); bgImage.color = new Color32(255, 255, 255, 20); if (index == 0) { swipyMenu.menus = new SwipyMenu.Menu[1]; } else { Array.Resize(ref swipyMenu.menus, swipyMenu.menus.Length + 1); } swipyMenu.menus[swipyMenu.menus.Length - 1].header = swipyMenuHeader; swipyMenu.menus[swipyMenu.menus.Length - 1].menu = currentMenu; if (firstToShowOnLoadMenu.header == null && firstToShowOnLoadMenu.menu == null) { firstToShowOnLoadMenu = swipyMenu.menus[0]; swipyMenu.defaultMenuIndex = 0; } ExpandHeaders(); Undo.CollapseUndoOperations(group); }