From db3ed28471cb33bfbc1fbc33bb56186433569300 Mon Sep 17 00:00:00 2001 From: Soraphis Date: Thu, 3 Aug 2023 23:14:26 +0200 Subject: [PATCH] AtomValueList disabled domain reload compatibility (#420) * fix: wrong state checked in AtomEvent reset (#403) * fix: make AtomValueList compatible with disabled domain reload (#419) fix: AtomValueList now resets runtime modifications (#418) * fix: handling dead references when resetting objects for AtomValueList. (#417) * removed the dead-reference handling (#417) - this will be addressed in (#421) compacted the Playmode state change --- Packages/Core/Runtime/Events/AtomEvent.cs | 11 +---- .../Core/Runtime/ValueLists/AtomValueList.cs | 45 +++++++++++++++++++ .../Core/Runtime/Variables/AtomVariable.cs | 4 +- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/Packages/Core/Runtime/Events/AtomEvent.cs b/Packages/Core/Runtime/Events/AtomEvent.cs index 43a9b37c..e7661c46 100644 --- a/Packages/Core/Runtime/Events/AtomEvent.cs +++ b/Packages/Core/Runtime/Events/AtomEvent.cs @@ -62,15 +62,8 @@ namespace UnityAtoms #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) + if (state == PlayModeStateChange.ExitingEditMode // BEFORE any GO is initialized: + || state == PlayModeStateChange.EnteredEditMode) // AFTER Playmode stopped { foreach (var instance in _instances) { diff --git a/Packages/Core/Runtime/ValueLists/AtomValueList.cs b/Packages/Core/Runtime/ValueLists/AtomValueList.cs index 5449cb7e..ef165fe9 100644 --- a/Packages/Core/Runtime/ValueLists/AtomValueList.cs +++ b/Packages/Core/Runtime/ValueLists/AtomValueList.cs @@ -1,6 +1,8 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Linq; +using UnityEditor; using UnityEngine; namespace UnityAtoms @@ -42,6 +44,49 @@ namespace UnityAtoms [SerializeField] protected List list = new List(); + private List _initial; + + +#if UNITY_EDITOR + /// + /// Set of all AtomVariable instances in editor. + /// + private static HashSet> _instances = new HashSet>(); +#endif + + protected virtual 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) // BEFORE any GO is initialized: + { + foreach (var instance in _instances) + { + if(instance._startCleared) instance.list.Clear(); + instance._initial = instance.list.ToList(); + } + } + else if (state == PlayModeStateChange.EnteredEditMode) // AFTER Playmode stopped + { + foreach (var instance in _instances) + { + instance.list = instance._initial; + } + } + } +#endif /// /// Add an item to the list. /// diff --git a/Packages/Core/Runtime/Variables/AtomVariable.cs b/Packages/Core/Runtime/Variables/AtomVariable.cs index 03ea9371..01170792 100644 --- a/Packages/Core/Runtime/Variables/AtomVariable.cs +++ b/Packages/Core/Runtime/Variables/AtomVariable.cs @@ -192,14 +192,14 @@ namespace UnityAtoms #if UNITY_EDITOR private static void HandlePlayModeStateChange(PlayModeStateChange state) { - if (state == PlayModeStateChange.ExitingEditMode) + if (state == PlayModeStateChange.ExitingEditMode) // BEFORE any GO is initialized: { foreach (var instance in _instances) { instance.SetInitialValues(); } } - else if (state == PlayModeStateChange.EnteredPlayMode) + else if (state == PlayModeStateChange.EnteredPlayMode) // within/end of the first frame { foreach (var instance in _instances) {