public void DropDown(Rect position, VisualElement targetElement = null, bool anchored = false) { if (targetElement == null) { Debug.LogError("VisualElement Generic Menu needs a target to find a root to attach to."); return; } m_PanelRootVisualContainer = targetElement.GetRootVisualContainer(); if (m_PanelRootVisualContainer == null) { Debug.LogError("Could not find rootVisualContainer..."); return; } m_PanelRootVisualContainer.Add(m_MenuContainer); m_MenuContainer.style.left = m_PanelRootVisualContainer.layout.x; m_MenuContainer.style.top = m_PanelRootVisualContainer.layout.y; m_MenuContainer.style.width = m_PanelRootVisualContainer.layout.width; m_MenuContainer.style.height = m_PanelRootVisualContainer.layout.height; var local = m_PanelRootVisualContainer.WorldToLocal(position); m_OuterContainer.style.left = local.x - m_PanelRootVisualContainer.layout.x; m_OuterContainer.style.top = local.y + position.height - m_PanelRootVisualContainer.layout.y; if (anchored) { m_DesiredRect = position; } m_MenuContainer.schedule.Execute(contentContainer.Focus); }
/// <summary> /// Displays the menu at the specified position. /// </summary> /// <remarks> /// This method automatically finds the parent VisualElement that displays the menu. /// For editor UI, <see cref="EditorWindow.rootVisualElement"/> is used as the parent. /// For runtime UI,<see cref="UIDocument.rootVisualElement"/> is used as the parent. /// </remarks> /// <param name="position">The position in the coordinate space of the panel.</param> /// <param name="targetElement">The element used to determine in which root to parent the menu.</param> /// <param name="anchored">Whether the menu should use the width of the position argument instead of its normal width.</param> public void DropDown(Rect position, VisualElement targetElement = null, bool anchored = false) { // TODO the argument should not optional. This is because IGenericMenu requires it, but this is not great. if (targetElement == null) { Debug.LogError("VisualElement Generic Menu needs a target to find a root to attach to."); return; } m_TargetElement = targetElement; m_PanelRootVisualContainer = m_TargetElement.GetRootVisualContainer(); if (m_PanelRootVisualContainer == null) { Debug.LogError("Could not find rootVisualContainer..."); return; } m_PanelRootVisualContainer.Add(m_MenuContainer); m_MenuContainer.style.left = m_PanelRootVisualContainer.layout.x; m_MenuContainer.style.top = m_PanelRootVisualContainer.layout.y; m_MenuContainer.style.width = m_PanelRootVisualContainer.layout.width; m_MenuContainer.style.height = m_PanelRootVisualContainer.layout.height; m_MenuContainer.style.fontSize = m_TargetElement.computedStyle.fontSize; m_MenuContainer.style.unityFont = m_TargetElement.computedStyle.unityFont; m_MenuContainer.style.unityFontDefinition = m_TargetElement.computedStyle.unityFontDefinition; var local = m_PanelRootVisualContainer.WorldToLocal(position); m_OuterContainer.style.left = local.x - m_PanelRootVisualContainer.layout.x; m_OuterContainer.style.top = local.y + position.height - m_PanelRootVisualContainer.layout.y; m_DesiredRect = anchored ? position : Rect.zero; m_MenuContainer.schedule.Execute(contentContainer.Focus); EnsureVisibilityInParent(); if (targetElement != null) { targetElement.pseudoStates |= PseudoStates.Active; } }