Skip to content

Commit

Permalink
fix: TogglablePreviewNode.Create can't be used in static init (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdunderscore authored Aug 4, 2024
1 parent f1f0a2f commit 1251a75
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Editor/PreviewSystem/TogglablePreviewNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using nadena.dev.ndmf.preview.UI;
using UnityEditor;

namespace nadena.dev.ndmf.preview
{
Expand All @@ -8,6 +9,14 @@ namespace nadena.dev.ndmf.preview
/// </summary>
public sealed class TogglablePreviewNode
{
private static bool IN_STATIC_INIT = true;

[InitializeOnLoadMethod]
private static void Init()
{
EditorApplication.delayCall += () => IN_STATIC_INIT = false;
}

/// <summary>
/// The name that will be shown to the user. Will be re-invoked on language change.
/// </summary>
Expand All @@ -22,20 +31,30 @@ private TogglablePreviewNode(Func<string> displayName, bool initialState)
}

/// <summary>
/// Creates a togglable preview node. Must not be invoked during static initialization.
/// Creates a togglable preview node.
/// </summary>
/// <param name="displayName">A function which returns the localized display name for this switch</param>
/// <param name="qualifiedName">If not null, a name which will be used to save this configuration</param>
/// <param name="initialState">The initial state for this node; defaults to true</param>
public static TogglablePreviewNode Create(Func<string> displayName, string qualifiedName = null,
bool initialState = true)
{
if (qualifiedName != null) initialState = PreviewPrefs.instance.GetNodeState(qualifiedName, initialState);

var node = new TogglablePreviewNode(displayName, initialState);


if (qualifiedName != null)
node.IsEnabled.OnChange += value => PreviewPrefs.instance.SetNodeState(qualifiedName, value);
{
EditorApplication.CallbackFunction loadSaved = () =>
{
node.IsEnabled.Value = PreviewPrefs.instance.GetNodeState(qualifiedName, initialState);
node.IsEnabled.OnChange += value => PreviewPrefs.instance.SetNodeState(qualifiedName, value);
};

if (IN_STATIC_INIT)
EditorApplication.delayCall += loadSaved;
else
loadSaved();
}

return node;
}
Expand Down

0 comments on commit 1251a75

Please sign in to comment.