fix: event replay buffer persisting if domain reload disabled

This commit is contained in:
Soraphis 2023-05-26 23:52:29 +02:00 committed by GitHub
parent 994026fb7e
commit e852be280a
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,6 +38,47 @@ 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();
}
}
else if (state == PlayModeStateChange.EnteredPlayMode)
{
foreach (var instance in _instances)
{
instance._replayBuffer.Clear();
}
}
}
#endif
private void OnDisable()
{
// Clear all delegates when exiting play mode