Skip to content

Commit

Permalink
Delay frame w unitask when regenerating children, closes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsarkar committed Feb 22, 2021
1 parent bcad789 commit 96f52b6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Editor/NeilSarkar.VioletUI.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "NeilSarkar.VioletUI.Editor.asmdef",
"references": [
"GUID:6f301220edcc84749a65b36e4025ea3a"
"GUID:6f301220edcc84749a65b36e4025ea3a",
"GUID:f51ebe6a0ceec4240a699833d6309b23"
],
"includePlatforms": [
"Editor"
Expand Down
4 changes: 3 additions & 1 deletion Editor/PrefabListener.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -36,11 +37,12 @@ static void UpdateRepeatViews(string prefabPath) {
if (processed.Contains(go.gameObject)) { continue; }

if (PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(repeatView.ViewPrefab) == prefabPath) {
repeatView.RegenerateChildren();
_ = repeatView.RegenerateChildren();
processed.Add(go.gameObject);
Violet.Log($"Updated RepeatView on {go.gameObject.name}");
}
}
}

}
}
3 changes: 2 additions & 1 deletion Runtime/IRepeatView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using Cysharp.Threading.Tasks;

public interface IRepeatView {
void RegenerateChildren();
UniTask RegenerateChildren();
GameObject ViewPrefab { get; }
}
13 changes: 5 additions & 8 deletions Runtime/Views/RepeatView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dispatch;
using UnityEngine;
using System.Collections;
using Cysharp.Threading.Tasks;
#if UNITY_EDITOR
using UnityEditor;
#endif
Expand Down Expand Up @@ -67,15 +68,11 @@ GameObject CreateChild(int index) {
/// Call RegenerateChildren in editor only to regenerate the children
/// when the child prefab is updated
/// </summary>
public void RegenerateChildren() {
StartCoroutine(RegenerateChildrenNextFrame());
}

IEnumerator RegenerateChildrenNextFrame() {
public async UniTask RegenerateChildren() {
#if UNITY_EDITOR
// 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;
await UniTask.DelayFrame(1);
Transform t = transform;
VioletScreen screen = default;
while (t != null) {
Expand All @@ -86,7 +83,7 @@ IEnumerator RegenerateChildrenNextFrame() {

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

var wasEditing = screen.isEditing;
Expand Down

0 comments on commit 96f52b6

Please sign in to comment.