Skip to content

Commit

Permalink
Wait a frame to regenerate repeatview children, and only unpack if wa…
Browse files Browse the repository at this point in the history
…s editing. Closes #1
  • Loading branch information
neilsarkar committed Feb 22, 2021
1 parent c1754f7 commit bcad789
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Runtime/Views/RepeatView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using Dispatch;
using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor;
#endif
Expand Down Expand Up @@ -62,8 +63,19 @@ GameObject CreateChild(int index) {
return Instantiate(ViewPrefab, transform);
}

/// <summary>
/// Call RegenerateChildren in editor only to regenerate the children
/// when the child prefab is updated
/// </summary>
public void RegenerateChildren() {
StartCoroutine(RegenerateChildrenNextFrame());
}

IEnumerator RegenerateChildrenNextFrame() {
// we have to wait a frame because PrefabListener is run as the asset is saving
// and the menu prefab won't be updated until the next frame
#if UNITY_EDITOR
yield return null;
Transform t = transform;
VioletScreen screen = default;
while (t != null) {
Expand All @@ -74,11 +86,11 @@ public void RegenerateChildren() {

if (screen == null) {
Violet.LogError($"Can't regenerate children bc screen is null. name={gameObject.name} parent={transform.parent}");
return;
yield break;
}

var wasEditing = screen.isEditing;
if (wasEditing) {
if (!wasEditing) {
screen.UnpackPrefab();
}
for (int i = transform.childCount - 1; i >= 0; i--) {
Expand All @@ -89,7 +101,7 @@ public void RegenerateChildren() {
CreateChild(i);
}

if (wasEditing) {
if (!wasEditing) {
screen.PackPrefab();
}
#else
Expand Down

0 comments on commit bcad789

Please sign in to comment.