Skip to content

Commit

Permalink
fix: Temporary variables in Prefs are reset when the Editor's PlayMod…
Browse files Browse the repository at this point in the history
…eState changes.
  • Loading branch information
fuqunaga committed Sep 25, 2024
1 parent 1622127 commit 6b14d9c
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Packages/ga.fuquna.prefsgui/Runtime/PrefsParam/PrefsParam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public virtual void Delete()
PrefsKvs.DeleteKey(key);
}

// 保持している一時変数などを初期化する
// EditorのPlayModeが変化したときに呼ばれる
public virtual void Reset()
{
ClearSync();
ClearCache();
}

public virtual void ClearCache()
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#if UNITY_EDITOR

using UnityEditor;

namespace PrefsGUI
{
/// <summary>
/// EditorのPlayModeが変化したときにstatic変数をクリアする
/// </summary>
[InitializeOnLoad]
public partial class PrefsParam
{
static PrefsParam()
{
EditorApplication.playModeStateChanged += OnPlayModeChanged;
}

private static void OnPlayModeChanged(PlayModeStateChange state)
{
if (state is PlayModeStateChange.ExitingPlayMode or PlayModeStateChange.ExitingEditMode)
{
foreach (var prefs in All)
{
prefs.Reset();
}

All.Clear();
AllDic.Clear();
KeyToOnValueChangedCallback.Clear();
}
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using PrefsGUI.Kvs;
using UnityEngine;
using UnityEngine.Assertions;

namespace PrefsGUI
Expand All @@ -15,19 +16,19 @@ public abstract class PrefsParamOuterInner<TOuter, TInner> : PrefsParamOuter<TOu

public class CachedValue<T>
{
private T value;
private T _value;

public bool HasValue { get; private set; }

public bool TryGet(out T v)
{
v = value;
v = _value;
return HasValue;
}

public void Set(T v)
{
value = v;
_value = v;
HasValue = true;
}

Expand All @@ -49,7 +50,7 @@ public void Clear()
#endregion


private static readonly Dictionary<string, OuterInnerCache> keyToCache = new();
private static readonly Dictionary<string, OuterInnerCache> KeyToCache = new();


private CachedValue<TInner> _defaultValueInnerCache = new();
Expand Down Expand Up @@ -124,7 +125,14 @@ protected bool SetInner(TInner v)


#region override


public override void Reset()
{
base.Reset();
_defaultValueInnerCache.Clear();
_prefsInnerAccessor = null;
}

public override void ClearCache()
{
base.ClearCache();
Expand All @@ -133,9 +141,9 @@ public override void ClearCache()

protected override void OnKeyChanged(string oldKey, string newKey)
{
if (!keyToCache.TryGetValue(newKey, out _cache))
if (!KeyToCache.TryGetValue(newKey, out _cache))
{
keyToCache[newKey] = _cache = new ();
KeyToCache[newKey] = _cache = new ();
}

base.OnKeyChanged(oldKey, newKey);
Expand Down Expand Up @@ -207,6 +215,7 @@ public PrefsInnerAccessor(PrefsParamOuterInner<TOuter, TInner> prefs)
private void OnValueChanged()
{
if (!_hasSyncedValue) return;
Debug.Log($"{Time.realtimeSinceStartup} {_prefs.key} {_prefs.Get()}");
_prefs.Synced = Equals(_syncedValue, Get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ protected set
}
}
}

private void ClearSync()
{
Synced = false;
onSyncedChanged = null;
}
}
}

0 comments on commit 6b14d9c

Please sign in to comment.