public override void OnInspectorGUI() { menuButton = target as CtxMenuButton; EditorGUIUtility.labelWidth = 120f; CtxMenu contextMenu = (CtxMenu)EditorGUILayout.ObjectField("Context Menu", menuButton.contextMenu, typeof(CtxMenu), true); if (menuButton.contextMenu != contextMenu) { RegisterUndo(); menuButton.contextMenu = contextMenu; } int sel = EditorGUILayout.IntField("Selected Item", menuButton.selectedItem); if (menuButton.selectedItem != sel) { RegisterUndo(); menuButton.selectedItem = sel; } UILabel label = (UILabel)EditorGUILayout.ObjectField("Current Item Label", menuButton.currentItemLabel, typeof(UILabel), true); if (menuButton.currentItemLabel != label) { RegisterUndo(); menuButton.currentItemLabel = label; } UISprite icon = (UISprite)EditorGUILayout.ObjectField("Current Item Icon", menuButton.currentItemIcon, typeof(UISprite), true); if (menuButton.currentItemIcon != icon) { RegisterUndo(); menuButton.currentItemIcon = icon; } NGUIEditorTools.DrawEvents("On Selection", menuButton, menuButton.onSelection); NGUIEditorTools.DrawEvents("On Show", menuButton, menuButton.onShow); NGUIEditorTools.DrawEvents("On Hide", menuButton, menuButton.onHide); if (menuButton.contextMenu != null) { EditMenuItemList(ref menuButton.menuItems, menuButton.contextMenu.atlas, true, ref menuButton.isEditingItems); } else { EditorGUILayout.HelpBox("You need to reference a context menu for this component to work properly.", MessageType.Warning); } if (GUI.changed) { EditorUtility.SetDirty(target); } }
public override void OnInspectorGUI() { menuButton = target as CtxMenuButton; EditorGUIUtility.labelWidth = 120f; CtxMenu contextMenu = (CtxMenu)EditorGUILayout.ObjectField("Context Menu", menuButton.contextMenu, typeof(CtxMenu), true); if (menuButton.contextMenu != contextMenu) { RegisterUndo(); menuButton.contextMenu = contextMenu; } int sel = EditorGUILayout.IntField("Selected Item", menuButton.selectedItem); if (menuButton.selectedItem != sel) { RegisterUndo(); menuButton.selectedItem = sel; } UILabel label = (UILabel)EditorGUILayout.ObjectField("Current Item Label", menuButton.currentItemLabel, typeof(UILabel), true); if (menuButton.currentItemLabel != label) { RegisterUndo(); menuButton.currentItemLabel = label; } UISprite icon = (UISprite)EditorGUILayout.ObjectField("Current Item Icon", menuButton.currentItemIcon, typeof(UISprite), true); if (menuButton.currentItemIcon != icon) { RegisterUndo(); menuButton.currentItemIcon = icon; } NGUIEditorTools.DrawEvents("On Selection", menuButton, menuButton.onSelection); NGUIEditorTools.DrawEvents("On Show", menuButton, menuButton.onShow); NGUIEditorTools.DrawEvents("On Hide", menuButton, menuButton.onHide); if (menuButton.contextMenu != null) { EditMenuItemList(ref menuButton.menuItems, menuButton.contextMenu.atlas, true, ref menuButton.isEditingItems); } else { EditorGUILayout.HelpBox("You need to reference a context menu for this component to work properly.",MessageType.Warning); } if (GUI.changed) EditorUtility.SetDirty(target); }
void OnMenuSelection() { selection = CtxMenu.current.selectedItem; // Update the button label and/or icon so that they agree with the // menu item selection. UpdateSelectionWidgets(); // Dispatch selection events. current = this; EventDelegate.Execute(onSelection); }
void OnPress(bool isPressed) { if (isPressed) { // We compute the menu position on the down-press. Why? Because // UIButtonScale and UIButtonOffset will distort the button's transform // and throw off our calculations if we do it on the up-press. We don't // want to not support those NGUI features, so... menuPosition = CtxHelper.ComputeMenuPosition(contextMenu, gameObject); } else if (enabled && contextMenu != null) { current = this; if (onShow != null) { EventDelegate.Execute(onShow); } CtxMenu.Item[] items = MenuItems; if (items != null || contextMenu.onShow != null) { EventDelegate.Add(contextMenu.onSelection, OnMenuSelection); EventDelegate.Add(contextMenu.onHide, OnHide, true); if (menuItems != null && menuItems.Length > 0) { contextMenu.Show(menuPosition, menuItems); } else { contextMenu.Show(menuPosition); } } } }
public static void AddMenuButton() { GameObject rootObj = NGUIMenu.SelectedRoot(); if (rootObj != null) { EditorUtility.SetDirty(rootObj); // Create a menu object. GameObject ctxMenuObj = new GameObject("Button Menu"); ctxMenuObj.layer = rootObj.layer; Transform ct = ctxMenuObj.transform; ct.parent = rootObj.transform; ct.localPosition = Vector3.zero; ct.localRotation = Quaternion.identity; ct.localScale = Vector3.one; CtxMenu menu = ctxMenuObj.AddComponent <CtxMenu>(); menu.atlas = PickAtlas(); menu.font = PickFont(); // Create button object. GameObject ctxButtonObj = new GameObject("Menu Button"); ctxButtonObj.layer = rootObj.layer; ct = ctxButtonObj.transform; ct.parent = rootObj.transform; ct.localPosition = Vector3.zero; ct.localRotation = Quaternion.identity; ct.localScale = Vector3.one; int depth = NGUITools.CalculateNextDepth(ctxButtonObj); // Create child objects. UISprite bg = NGUITools.AddSprite(ctxButtonObj, PickAtlas(), "Button"); bg.name = "Background"; bg.depth = depth; bg.width = 150; bg.height = 40; bg.MakePixelPerfect(); UILabel lbl = NGUITools.AddWidget <UILabel>(ctxButtonObj); lbl.bitmapFont = PickFont(); lbl.text = ctxButtonObj.name; lbl.color = Color.black; lbl.MakePixelPerfect(); Vector2 size = lbl.printedSize; // Force NGUI to process metrics before adding collider. Otherwise you get incorrect-sized collider. size.x -= 1f; // Supress compiler warning for unused 'size' variable. Sheesh... // Attach button and menu components. NGUITools.AddWidgetCollider(ctxButtonObj, true); CtxMenuButton menuButton = ctxButtonObj.AddComponent <CtxMenuButton>(); menuButton.contextMenu = menu; menuButton.currentItemLabel = lbl; ctxButtonObj.AddComponent <UIButton>().tweenTarget = bg.gameObject; ctxButtonObj.AddComponent <UIButtonScale>(); ctxButtonObj.AddComponent <UIButtonOffset>(); ctxButtonObj.AddComponent <UIPlaySound>(); Selection.activeGameObject = ctxButtonObj; Undo.RegisterCreatedObjectUndo(ctxButtonObj, "Add a Menu Button"); } }
void OnPress(bool isPressed) { if (isPressed) { // We compute the menu position on the down-press. Why? Because // UIButtonScale and UIButtonOffset will distort the button's transform // and throw off our calculations if we do it on the up-press. We don't // want to not support those NGUI features, so... menuPosition = CtxHelper.ComputeMenuPosition(contextMenu, gameObject); } else if (enabled && contextMenu != null) { current = this; if (onShow != null) EventDelegate.Execute(onShow); CtxMenu.Item[] items = MenuItems; if (items != null || contextMenu.onShow != null) { EventDelegate.Add(contextMenu.onSelection, OnMenuSelection); EventDelegate.Add(contextMenu.onHide, OnHide, true); if (menuItems != null && menuItems.Length > 0) contextMenu.Show(menuPosition, menuItems); else contextMenu.Show(menuPosition); } } }