Consider a situation where we wish to change the number of slots. An alternative to using scrollbars is changing the cell size in the Grid Layout Group component. We can automate this through code so that the cell size is changed to ensure that NUM_INVENTORY_SLOTS will fit the width of our panel, at the top of the canvas.
To automatically resize the cell size of Grid Layout Group for this recipe, we need to do the following:
- Comment out the third statement in the Awake() method for the C# PlayerInventoryDisplay script class:
// panelSlotGrid.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(
// RectTransform.Axis.Horizontal, width);
- Add the following Start() method to PlayerInventoryDisplay in the player-girl1 GameObject with the following code:
void Start() { float panelWidth = panelSlotGrid.GetComponent<RectTransform>
...