cz.xprees.variables 1.0.20 → 1.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@
3
3
  // ------------------------------------------------------------------------------------------------------
4
4
 
5
5
  using System;
6
+ using UnityEngine;
6
7
  using UnityEngine.Events;
7
8
  using Xprees.Core;
8
9
  using Xprees.Variables.Utils;
@@ -14,14 +15,35 @@ namespace Xprees.Variables.Base
14
15
  /// </summary>
15
16
  /// <typeparam name="T">Unity Serializable</typeparam>
16
17
  [Serializable]
17
- public class ReferenceBase<T> : IResettable
18
+ public class ReferenceBase<T> : IResettable, ISerializationCallbackReceiver
18
19
  {
20
+ private bool _hasCapturedBaseline; // Used to check if baseline has been captured
19
21
  private T _defaultInlinedValue; // Used to reset state of inlined value
20
22
 
21
23
  public bool useInlined = true;
22
24
  public T inlinedValue;
23
25
  public VariableBaseSO<T> variable;
24
26
 
27
+ public void OnBeforeSerialize()
28
+ {
29
+ }
30
+
31
+ public void OnAfterDeserialize()
32
+ {
33
+ if (!PlayModeStateTracker.IsPlaying)
34
+ {
35
+ _defaultInlinedValue = CloningTools.Clone(inlinedValue);
36
+ _hasCapturedBaseline = false;
37
+ return;
38
+ }
39
+
40
+ if (!_hasCapturedBaseline)
41
+ {
42
+ _defaultInlinedValue = CloningTools.Clone(inlinedValue);
43
+ _hasCapturedBaseline = true;
44
+ }
45
+ }
46
+
25
47
  // Internal event for inlined value changes, since VariableBaseSO already has its own onValueChanged event.
26
48
  private UnityAction<T> _onInlinedValueChanged;
27
49
 
@@ -31,7 +53,7 @@ namespace Xprees.Variables.Base
31
53
  {
32
54
  add
33
55
  {
34
- var shouldRedirectToVarEvent = !useInlined && variable != null;
56
+ var shouldRedirectToVarEvent = !useInlined && variable;
35
57
  if (shouldRedirectToVarEvent)
36
58
  {
37
59
  variable.onValueChanged += value;
@@ -43,7 +65,7 @@ namespace Xprees.Variables.Base
43
65
 
44
66
  remove
45
67
  {
46
- var shouldRedirectToVarEvent = !useInlined && variable != null;
68
+ var shouldRedirectToVarEvent = !useInlined && variable;
47
69
  if (shouldRedirectToVarEvent)
48
70
  {
49
71
  variable.onValueChanged -= value;
@@ -63,6 +85,7 @@ namespace Xprees.Variables.Base
63
85
  useInlined = true;
64
86
  inlinedValue = value;
65
87
  _defaultInlinedValue = value;
88
+ _hasCapturedBaseline = true;
66
89
  }
67
90
 
68
91
  public T Value
@@ -89,6 +112,7 @@ namespace Xprees.Variables.Base
89
112
  if (!useInlined) return; // Variables does that by themselves
90
113
 
91
114
  _defaultInlinedValue = CloningTools.Clone(inlinedValue);
115
+ _hasCapturedBaseline = true;
92
116
  }
93
117
 
94
118
  public virtual void ResetState()
@@ -43,6 +43,13 @@ namespace Xprees.Variables.Base
43
43
  get => currentValue;
44
44
  set
45
45
  {
46
+ #if UNITY_EDITOR
47
+ // If not already captured in Editor capture before changing the value
48
+ if (PlayModeStateTracker.IsPlaying)
49
+ {
50
+ StateSnapshotService.EnsureCaptured(this);
51
+ }
52
+ #endif
46
53
  currentValue = value;
47
54
  onValueChanged?.Invoke(value);
48
55
  }
@@ -38,9 +38,14 @@ namespace Xprees.Variables.Base
38
38
 
39
39
  public override void ResetState()
40
40
  {
41
- base.ResetState();
42
- variable?.ResetState();
41
+ if (protectedDontReset) return;
42
+ ForceResetState();
43
+ }
44
+
45
+ public override void ForceResetState()
46
+ {
43
47
  disableWrite?.ResetState();
48
+ variable?.ResetState();
44
49
  }
45
50
  }
46
51
  }
@@ -1,4 +1,4 @@
1
- using System;
1
+ using System;
2
2
  using System.Collections;
3
3
  using System.Runtime.CompilerServices;
4
4
  using UnityEngine;
@@ -24,6 +24,13 @@ namespace Xprees.Variables.Utils
24
24
  var canReturnAsIs = typeof(T).IsValueType && !RuntimeHelpers.IsReferenceOrContainsReferences<T>();
25
25
  if (canReturnAsIs) return value;
26
26
 
27
+ // Strings are immutable in .NET, so return directly without JsonUtility
28
+ if (typeof(T) == typeof(string)) return value;
29
+
30
+ // UnityEngine.Object references (ScriptableObjects, MonoBehaviours, Assets, GameObjects)
31
+ // are reference-assigned; we must not deep-clone or call CreateInstance during serialization.
32
+ if (typeof(Object).IsAssignableFrom(typeof(T)) || value is Object) return value;
33
+
27
34
  if (value is ICloneable cloneable) return (T) cloneable.Clone();
28
35
 
29
36
  // Handle collections (List<T>, arrays, etc.) - JsonUtility doesn't support them directly
@@ -96,6 +103,9 @@ namespace Xprees.Variables.Utils
96
103
  // Value types are copied by value
97
104
  if (itemType.IsValueType) return item;
98
105
 
106
+ // Strings are immutable
107
+ if (item is string) return item;
108
+
99
109
  // ICloneable
100
110
  if (item is ICloneable cloneable) return cloneable.Clone();
101
111
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "cz.xprees.variables",
3
3
  "displayName": "Variables",
4
- "version": "1.0.20",
4
+ "version": "1.0.22",
5
5
  "unity": "2021.3",
6
6
  "description": "This package contains a system for creating variables based on ScriptableObjects in Editor.",
7
7
  "license": "Apache-2.0",