Skip to content

Commit

Permalink
Fix bug not save shared variables modification in the BehaviorTreeCom…
Browse files Browse the repository at this point in the history
…ponent Inspector
  • Loading branch information
AkiKurisu committed Jul 6, 2024
1 parent edbb99d commit 4eb0504
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions Editor/Core/GraphView/SharedVariablesFoldout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
namespace Kurisu.AkiBT.Editor
{
public class SharedVariablesFoldout : Foldout
{
private readonly HashSet<ObserveProxyVariable> observeProxies;
public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target, UnityEditor.Editor editor)
private readonly HashSet<ObservableVariable> observableVariables;
public SharedVariablesFoldout(IVariableSource source, System.Action onUpdate)
{
value = false;
text = "SharedVariables";
RegisterCallback<DetachFromPanelEvent>(Release);
observeProxies = new HashSet<ObserveProxyVariable>();
observableVariables = new HashSet<ObservableVariable>();
var factory = FieldResolverFactory.Instance;
foreach (var variable in source.SharedVariables.Where(x => x.IsExposed))
{
Expand All @@ -35,7 +34,7 @@ public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target,
{
var index = source.SharedVariables.FindIndex(x => x.Name == variable.Name);
source.SharedVariables[index].SetValue(obj);
NotifyEditor();
onUpdate();
});
if (Application.isPlaying)
{
Expand All @@ -45,7 +44,7 @@ public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target,
//Disable since you should only edit global variable in source
if (variable.IsGlobal) valueField.SetEnabled(false);
valueField.tooltip = "Global variable can only be edited in source at runtime";
observeProxies.Add(observeProxy);
observableVariables.Add(observeProxy);
}
if (valueField is TextField field)
{
Expand Down Expand Up @@ -79,7 +78,7 @@ public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target,
{
variable.IsGlobal = !variable.IsGlobal;
SetToggleButtonColor(globalToggle, variable.IsGlobal);
NotifyEditor();
onUpdate();
};
}
SetToggleButtonColor(globalToggle, variable.IsGlobal);
Expand All @@ -97,7 +96,7 @@ public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target,
{
RemoveFromHierarchy();
}
NotifyEditor();
onUpdate();
})
{
text = "Delate"
Expand All @@ -111,21 +110,16 @@ public SharedVariablesFoldout(IVariableSource source, UnityEngine.Object target,
//Append to folder
Add(grid);
}
void NotifyEditor()
{
EditorUtility.SetDirty(target);
EditorUtility.SetDirty(editor);
AssetDatabase.SaveAssets();
}
void SetToggleButtonColor(Button button, bool isOn)

static void SetToggleButtonColor(Button button, bool isOn)
{
button.style.color = isOn ? Color.green : Color.gray;
}
}

private void Release(DetachFromPanelEvent _)
{
foreach (var proxy in observeProxies)
foreach (var proxy in observableVariables)
{
proxy.Dispose();
}
Expand Down

0 comments on commit 4eb0504

Please sign in to comment.