From bd0df2aaabe915f7b5049af4bdd93bb9c741a3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Sch=C3=B6ner?= <tobias.stonehead@gmail.com> Date: Thu, 2 Mar 2023 12:04:19 +0100 Subject: [PATCH] fix: FactExplorer also spawns at correct location on scaled canvases --- Assets/Scripts/UI/FactExplorer/FactExplorer.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs index c23cdad9..b956c23c 100644 --- a/Assets/Scripts/UI/FactExplorer/FactExplorer.cs +++ b/Assets/Scripts/UI/FactExplorer/FactExplorer.cs @@ -86,16 +86,20 @@ private void DestroyIfClickedOutside() private void MoveToPreferredPosition(Vector3 prefPos) { LayoutRebuilder.ForceRebuildLayoutImmediate(transform.GetComponent<RectTransform>()); - // calculate opimal position + // calculate optimal position var deltaPos = mainFactUI.position - prefPos; transform.position -= deltaPos; // clamp position, so that no parts of the FactExplorer are out of screen - RectTransform feRect = GetComponent<RectTransform>(); - Vector2 apos = feRect.anchoredPosition; - apos.x = Mathf.Clamp(apos.x, 0, Screen.width - feRect.sizeDelta.x); - apos.y = Mathf.Clamp(apos.y, -Screen.height + feRect.sizeDelta.y, 0); - feRect.anchoredPosition = apos; + RectTransform rect = GetComponent<RectTransform>(); + RectTransform canvasRect = GetComponentInParent<Canvas>().transform.GetComponent<RectTransform>(); + + var sizeDelta = canvasRect.sizeDelta - rect.sizeDelta; + var panelPivot = rect.pivot; + var position = rect.anchoredPosition; + position.x = Mathf.Clamp(position.x, -sizeDelta.x * panelPivot.x, sizeDelta.x * (1 - panelPivot.x)); + position.y = Mathf.Clamp(position.y, -sizeDelta.y * panelPivot.y, sizeDelta.y * (1 - panelPivot.y)); + rect.anchoredPosition = position; } #endregion Implementation -- GitLab