0

I would like to call the UpdateCollection () method in the script in order to generate a certain number of buttons that I previously created dynamically.

Can someone tell me how I can use the UpdateCollection () method in the script? You can easily execute the method via the Inspector, now you want to use the method in the script.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ektukl
  • 1
  • 1

2 Answers2

1

First of all, you have to either create a public variable public ScrollingObjectCollection scrollingCollection; in your script and assign the component in the Unity-Editor or use GameObject.Find("MyGameObjectName").GetComponent<ScrollingObjectCollection>() to assign the variable in the Start() method.

After instantiating your buttons you can call the scrollingCollection.UpdateContent() method.

However, if you are using a GridObjectCollection inside your ScrollingObjectCollection in order to layout the children you have to create a variable of that type aswell, assign the GridObjectCollection script and call objectCollection.UpdateCollection() before calling scrollingCollection.UpdateContent().

As this answer states, this should be done inside a coroutine on order to work as expected.

Here's an excerpt from my code:

public GridObjectCollection objectCollection;
public ScrollingObjectCollection scrollingCollection;

private void Start()
{
    InsertIntoObjectCollection(20);
}

public void InsertIntoObjectCollection(int count)
{
    StartCoroutine(InsertIntoObjectCollection_Coroutine(count));
}

public IEnumerator InsertIntoObjectCollection_Coroutine(int count)
{
    for (int i = 0; i < count; i++)
    {
        var newErrorMessage = Instantiate(singleErrorMessagePrefab, objectCollection.transform);
    }
    yield return new WaitForEndOfFrame();
    objectCollection.UpdateCollection();
    yield return new WaitForEndOfFrame();
    scrollingCollection.UpdateContent();
}
Adrian
  • 31
  • 1
0

I think the method UpdateContent from ScrollingObjectCollection class has the same purpose as UpdateCollection from the Editor.

Here's the description about this function:

Sets up the scroll clipping object and the interactable components according to the scroll content and chosen settings.

Doc link:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.mixedreality.toolkit.ui.scrollingobjectcollection.updatecontent?view=mixed-reality-toolkit-unity-2020-dotnet-2.7.0#Microsoft_MixedReality_Toolkit_UI_ScrollingObjectCollection_UpdateContent

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Alexis AG
  • 1
  • 1