0
\$\begingroup\$

I have this script here that generates buttons based on an array and a serialized grid transform

public void InitializeUI()
{
    LevelItem[] levelItemsArray = LevelSystemManager.Instance.LevelData.levelItemArray;
    for (int i = 0; i < levelItemsArray.Length; i++)
    {
        if (i % n == 0) // sets it to 15
        {
            LevelButtonScript levelButton = Instantiate(levelBtnPrefab, levelBtnGridHolder);
            levelButton.SetLevelButton(levelItemsArray[i], i, i == LevelSystemManager.Instance.LevelData.lastUnlockedLevel);
            var page = GameObject.FindObjectOfType<UI.Pagination.PagedRect>();
            page.AddPageUsingTemplate();
        }
    }
}

Now what I'm trying to do is make that array instantiate a new page (calling the page.AddPageUsingTemplate() function which is from another script) once for every 15 items of the array.

I have 75 levels and want to instantiate the buttons on a new game object which has a grid layout group, so it would be 15 buttons on each grid with 5 pages/grids in total.

I do indeed get 5 pages when I run this code, but since the transform is only set to the first one the buttons don't get placed on the other pages.

How can I place buttons after the first 15 on the following pages?

\$\endgroup\$
2
  • \$\begingroup\$ Can you show us in the hierarchy/inspector, where the grid object sits relative to the page created by AddPageUsingTemplate? Could you set up AddPageUsingTemplate to return a reference to this grid object? \$\endgroup\$
    – DMGregory
    Commented Apr 18, 2021 at 20:48
  • \$\begingroup\$ this function uses an inactive page in the scroll view object that has a panel as a child, that panel has the layout grid component and is set to the correct dimensions so it can fit 15 buttons. The Hierarchy is the LevelSelector GameObject => Slider-Scrollrect => Viewport => Content = > Page Template which has a panel child that contains the grid layout. As for using the AddPageUsingTemplate function all it does is to instantiate a new page prefab " Instantiate(NewPageTemplate) as Page; " . \$\endgroup\$
    – stratos la
    Commented Apr 18, 2021 at 20:57

1 Answer 1

0
\$\begingroup\$

First, I'd update your AddPageUsingTemplate function to return a reference to the container that should be used to add buttons to the newly-spawned page. Something like this:

[SerializeField] Page newPageTemplate;

public RectTransform AddPageUsingTemplate() {
    var newPage = Instantiate(NewPageTemplate);
    return (RectTransform)newPage.transform;
}

Then we can use this to update our "current page" as we make our buttons:

LevelItem[] levelItemsArray = LevelSystemManager.Instance.LevelData.levelItemArray;

// Cache the initial container as our first page.
RectTransform currentPage = levelBtnGridHolder;
int buttonsPerPage = 15;

// Do this once, not repeatedly in the loop.
var page = GameObject.FindObjectOfType<UI.Pagination.PagedRect>();

for (int i = 0; i < levelItemsArray.Length; i++)
{
    // Every 15 buttons, start a new page, and save it as our current page.
    if (i % buttonsPerPage == 0)
    {            
        currentPage = page.AddPageUsingTemplate();
    }

    // Add button to the current/newly-created page.
    LevelButtonScript levelButton = Instantiate(levelBtnPrefab, lurrentPage;
    levelButton.SetLevelButton(levelItemsArray[i], i, i == LevelSystemManager.Instance.LevelData.lastUnlockedLevel);
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much! to be honest i followed your example on my level class since the Page class was a bit different . but i did everything you said expect i set the currentPage = page.AddPageUsingTemplate().transform. also i removed the panel child object from the template and used that one as the grid layout! \$\endgroup\$
    – stratos la
    Commented Apr 18, 2021 at 21:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .