MRTK 2.4 - ScrollingObjectCollection 不通过脚本更新

问题描述

我在我的场景中创建了一个通知面板,它会在发生某些事情时显示通知。这些通知只是作为子项添加ScrollingObjectCollection:

enter image description here

我遇到的问题是,删除一个通知(中间那个)后,集合没有更新。但是单击 UpdateCollection-Btn 上的检查器将更新集合。我调试了一下,发现是知道删了一条后,只剩下两条通知了。

private void DeleteNotification()
{
    if (NotifyManager.Instance.RemoveNotificationFromList(Id))
    {
        Destroy(this.gameObject);
        NotifyManager.Instance.ScrollingCollection.UpdateCollection();
    }
}

enter image description here



这是 Inspector 中 UpdateCollection 中的按钮 ScrollingObjectCollection调用的,它的方法也与我调用方法相同:

if (GUILayout.Button("Update Collection"))
{
    scrollContainer.UpdateCollection();
    EditorUtility.SetDirty(scrollContainer);
}

有人知道如何通过代码在运行时更新该集合吗?

我的设置:

  • Unity 2019.4.18
  • MRTK 2.4

解决方法

好的,我自己修好了。问题是这一切都发生在一帧中,而 ScrollingObjectCollection 并未处于通知被删除的状态。因此,我在 NotifyManager 中创建了另一个方法,在该方法中等待帧结束,然后调用 UpdateCollection

private IEnumerator WaitBeforeUpdateCollection()
{
    yield return new WaitForEndOfFrame();
    scrollingCollection.UpdateCollection();
}