fix: Event Replay Buffer not cleared when domain reload disabled (#403)

* fix: event replay buffer persisting if domain reload disabled

* fix: clearing event registrations when changing playmode stage.
This commit is contained in:
Soraphis 2023-07-19 19:47:43 +02:00 committed by GitHub
parent b225290b48
commit 62bc3c9812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,9 @@
using System;
using System.Linq;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
namespace UnityAtoms
@ -35,17 +38,53 @@ namespace UnityAtoms
private Queue<T> _replayBuffer = new Queue<T>();
#if UNITY_EDITOR
/// <summary>
/// Set of all AtomVariable instances in editor.
/// </summary>
private static HashSet<AtomEvent<T>> _instances = new HashSet<AtomEvent<T>>();
#endif
private void OnEnable()
{
#if UNITY_EDITOR
if (EditorSettings.enterPlayModeOptionsEnabled)
{
_instances.Add(this);
EditorApplication.playModeStateChanged -= HandlePlayModeStateChange;
EditorApplication.playModeStateChanged += HandlePlayModeStateChange;
}
#endif
}
#if UNITY_EDITOR
private static void HandlePlayModeStateChange(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingEditMode)
{
foreach (var instance in _instances)
{
instance._replayBuffer.Clear();
instance.UnregisterAll();
}
}
else if (state == PlayModeStateChange.EnteredPlayMode)
{
foreach (var instance in _instances)
{
instance._replayBuffer.Clear();
instance.UnregisterAll();
}
}
}
#endif
private void OnDisable()
{
// Clear all delegates when exiting play mode
if (_onEvent != null)
{
var invocationList = _onEvent.GetInvocationList();
foreach (var d in invocationList)
{
_onEvent -= (Action<T>)d;
}
}
UnregisterAll();
}
/// <summary>