Question by
fischma · May 01 at 09:10 AM ·
uicanvaspositioning
UI Canvas position issues
I have several problems with positioning canvas elements via c# script. The text is too small. The image element and text element are moved up from the parent container. For example, although I position Y as 0 in the code, the text element after starting at the Y coordinate has 576.7371
private void SetPositionForItemGroup(RectTransform rect, RectTransform rectParent)
{
rect.anchoredPosition = rectParent.position;
rect.anchorMin = new Vector2(0, 1);
rect.anchorMax = new Vector2(0, 1);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(rectParent.rect.size.x, 50);
}
private void SetPositionForItemIcon(RectTransform rect, RectTransform rectParent)
{
// rect.anchoredPosition = rectParent.position;
rect.anchorMin = new Vector2(0, 0.5f);
rect.anchorMax = new Vector2(0, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(50, 50); // width and height
rect.transform.position = new Vector3(50, 0, 0); // padding left 50
}
private void SetPositionForItemText(RectTransform rect, RectTransform rectParent)
{
// rect.anchoredPosition = rectParent.position;
rect.anchorMin = new Vector2(0, 0.5f);
rect.anchorMax = new Vector2(0, 0.5f);
rect.pivot = new Vector2(0.5f, 0.5f);
rect.sizeDelta = new Vector2(rectParent.rect.size.x - 105, 50); // width and height
rect.transform.position = new Vector3(255, 0, 0); // padding left 255
}
Thanks
unityui-1.png
(177.4 kB)
Comment