Hello everyone!
I want to adjust my field of buttons(4x4) to different aspect ratios per script. If I create the buttons manually and set the canvas as parent, everything looks correct, but not if I create it via script.
My Startfunction looks like this:
buttonSize = (Screen.width * buttonSizeStandard) / 1080;
buttonOffset = (Screen.width * buttonOffsetStandard) / 1080;
And the function to initialize my 4x4 field looks like this:
var borderCanX = (this.gameObject.GetComponent<RectTransform>().sizeDelta.x - ((buttonSize * 4) + (buttonOffset * 3))) / 2;
var borderCanY = (this.gameObject.GetComponent<RectTransform>().sizeDelta.y - ((buttonSize * 4) + (buttonOffset * 3))) / 2;
var offset = new Vector3((this.gameObject.GetComponent<RectTransform>().sizeDelta.x / 2) - (fieldButton.GetComponent<RectTransform>().sizeDelta.x / 2) - borderCanX, this.gameObject.GetComponent<RectTransform>().sizeDelta.y / 2 - (fieldButton.GetComponent<RectTransform>().sizeDelta.y / 2) - borderCanY);
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
var btn = Instantiate(fieldButton, new Vector3(0, 0, 0), Quaternion.identity);
btn.transform.SetParent(this.gameObject.transform);
btn.GetComponent<RectTransform>().anchoredPosition = new Vector3((x * (buttonSize + buttonOffset)) - offset.x, (y * (buttonSize + buttonOffset)) - offset.y);
btn.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonSize, buttonSize);
}
}
My Canvas has this settings:
It would be great if somebody could help me!
Best wishes!