public void SetCenterPosition(Vector2 screenPoint) { var flippedScreenPoint = new Vector2(screenPoint.x, Screen.height - screenPoint.y); var panelPoint = RuntimePanelUtils.ScreenToPanel(m_UiRoot.rootVisualElement.panel, flippedScreenPoint); m_Container.style.left = panelPoint.x; m_Container.style.top = panelPoint.y; }
static void MoveAndScaleToWorldPosition(VisualElement element, Vector3 worldPosition, Vector2 worldSize) { Rect rect = RuntimePanelUtils.CameraTransformWorldToPanelRect(element.panel, worldPosition, worldSize, Camera.main); Vector2 layoutSize = element.layout.size; // Don't set scale to 0 or a negative number. Vector2 scale = layoutSize.x > 0 && layoutSize.y > 0 ? rect.size / layoutSize : Vector2.one * 1e-5f; element.transform.position = rect.position; element.transform.scale = new Vector3(scale.x, scale.y, 1); }
private void UpdateCache() { cacheFrame = Time.frameCount; Vector2 screenSize = new(Screen.width, Screen.height); Vector2 stpMin = RuntimePanelUtils.ScreenToPanel(panel, new Vector2(0, 0)); Vector2 stpMax = RuntimePanelUtils.ScreenToPanel(panel, screenSize); Vector2 stpSize = stpMax - stpMin; screenHeight = screenSize.y; panelScreenMin = stpMin; scalingRatio = screenSize / stpSize; }
private void LayoutChanged(GeometryChangedEvent e) { var safeArea = Screen.safeArea; try { var leftTop = RuntimePanelUtils.ScreenToPanel(panel, new Vector2(safeArea.xMin, Screen.height - safeArea.yMax)); var rightBottom = RuntimePanelUtils.ScreenToPanel(panel, new Vector2(Screen.width - safeArea.xMax, safeArea.yMin)); style.marginLeft = leftTop.x; style.marginTop = leftTop.y; style.marginRight = rightBottom.x; style.marginBottom = rightBottom.y; } catch (InvalidCastException) {} }
void OnGeometryChanged(GeometryChangedEvent e) { var panel = this.panel; #if UNITY_EDITOR if (panel.contextType == ContextType.Editor) { //TODO: Is it possible to do the same in the editor? return; } #endif var safeArea = Screen.safeArea; var lt = RuntimePanelUtils.ScreenToPanel(panel, new Vector2(safeArea.xMin, Screen.height - safeArea.yMax)); var rb = RuntimePanelUtils.ScreenToPanel(panel, new Vector2(Screen.width - safeArea.xMax, safeArea.yMin)); { this.style.paddingLeft = lt.x; this.style.paddingTop = lt.y; this.style.paddingRight = rb.x; this.style.paddingBottom = rb.y; } }
public Vector2 ScreenToPanel(Vector2 v) { return(RuntimePanelUtils.ScreenToPanel(panel, v)); }
/// <summary> /// Set the position of the UI object /// </summary> public void SetPosition() { Vector2 newPosition = RuntimePanelUtils.CameraTransformWorldToPanel(m_Bar.panel, TransformToFollow.position, m_MainCamera); m_Bar.transform.position = newPosition.WithNewX(newPosition.x - m_Bar.layout.width / 2); }