Skip to content
Snippets Groups Projects
Commit bd0df2aa authored by Tobias Schöner's avatar Tobias Schöner
Browse files

fix: FactExplorer also spawns at correct location on scaled canvases

parent 4b9f34bb
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment