Search code examples
c#unity-game-engineunity-ui

Spawned Unity UI Grid Layout child object is not correct size


I am using unity grid layout component but not working correctly.

I am using unity grid layout component grid layout values. When I drag and drop my prefab as a child of grid layout object is perfectly normal working: normal but when I use this code for spawning, starts not fitting: not size correctly

Code:

GameObject temp = Instantiate(myprefab, new Vector3(0, 0, 0), Quaternion.identity);
temp.transform.SetParent(mylayout.transform);

But when I setting game view full screen and running code its change again its changing with game screen size different size same code.

Canvas Values


Solution

  • Yo I can use LayoutBuilder to force a rebuild after adding your grid element. Pas the parent recttransform as parameter.