diff --git a/Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs b/Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs index 6c506b4..bfda5e8 100644 --- a/Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs +++ b/Packages/se.hertzole.scriptable-values/Runtime/Values/ValueReference.cs @@ -46,7 +46,7 @@ public class ValueReference public T Value { get { return GetValue(); } - set { SetValue(value); } + set { SetValue(value, true); } } internal event ScriptableValue.OldNewValue OnValueChangingInternal; @@ -181,7 +181,7 @@ private T GetValue() } } - private void SetValue(T value) + private void SetValue(T value, bool notify) { T previousValue = Value; if (EqualityHelper.Equals(previousValue, value)) @@ -192,24 +192,50 @@ private void SetValue(T value) switch (valueType) { case ValueReferenceType.Constant: - OnValueChangingInternal?.Invoke(previousValue, value); + if (notify) + { + OnValueChangingInternal?.Invoke(previousValue, value); + } constantValue = value; - OnValueChangedInternal?.Invoke(previousValue, value); + + if (notify) + { + OnValueChangedInternal?.Invoke(previousValue, value); + } break; case ValueReferenceType.Reference: - referenceValue.Value = value; + if (notify) + { + referenceValue.Value = value; + } + else + { + referenceValue.SetValueWithoutNotify(value); + } break; #if SCRIPTABLE_VALUES_ADDRESSABLES case ValueReferenceType.Addressable: if (AssetHandle.IsValid() && AssetHandle.IsDone && AssetHandle.Result != null) { - AssetHandle.Result.Value = value; + if (notify) + { + AssetHandle.Result.Value = value; + } + else + { + AssetHandle.Result.SetValueWithoutNotify(value); + } } break; #endif } } + + public void SetValueWithoutNotify(T value) + { + SetValue(value, false); + } #if UNITY_EDITOR internal void SetPreviousValue() diff --git a/Packages/se.hertzole.scriptable-values/Tests/Runtime/BaseValueReferenceTest.cs b/Packages/se.hertzole.scriptable-values/Tests/Runtime/BaseValueReferenceTest.cs index 74ffbf8..c1cf7a8 100644 --- a/Packages/se.hertzole.scriptable-values/Tests/Runtime/BaseValueReferenceTest.cs +++ b/Packages/se.hertzole.scriptable-values/Tests/Runtime/BaseValueReferenceTest.cs @@ -255,6 +255,79 @@ public void InvalidType_Set() Assert.IsTrue(gotError); } + [Test] + public void SetValueWithoutNotify_Constant() + { + bool eventInvoked = false; + + ValueReference instance = new ValueReference(default(TValue)); + instance.OnValueChanged += ValueChanged; + + instance.SetValueWithoutNotify(MakeDifferentValue(default)); + + Assert.AreEqual(MakeDifferentValue(default), instance.constantValue); + Assert.AreEqual(MakeDifferentValue(default), instance.Value); + Assert.IsFalse(eventInvoked); + + eventInvoked = false; + + instance.OnValueChanged -= ValueChanged; + + TValue differentValue = MakeDifferentValue(instance.Value); + + instance.SetValueWithoutNotify(differentValue); + + Assert.AreEqual(differentValue, instance.constantValue); + Assert.AreEqual(differentValue, instance.Value); + Assert.IsFalse(eventInvoked); + + void ValueChanged(TValue previousValue, TValue newValue) + { + Assert.AreEqual(MakeDifferentValue(default), previousValue); + Assert.AreEqual(MakeDifferentValue(default), newValue); + eventInvoked = true; + } + } + + [Test] + public void SetValueWithoutNotify_Reference() + { + bool eventInvoked = false; + + TType scriptableValue = CreateInstance(); + scriptableValue.Value = default; + + ValueReference instance = new ValueReference(scriptableValue); + instance.OnValueChanged += ValueChanged; + + instance.SetValueWithoutNotify(MakeDifferentValue(default)); + + Assert.AreEqual(default, instance.constantValue); + Assert.AreEqual(MakeDifferentValue(default), instance.Value); + Assert.AreEqual(MakeDifferentValue(default), scriptableValue.Value); + Assert.IsFalse(eventInvoked); + + eventInvoked = false; + + instance.OnValueChanged -= ValueChanged; + + TValue differentValue = MakeDifferentValue(instance.Value); + + instance.SetValueWithoutNotify(differentValue); + + Assert.AreEqual(default, instance.constantValue); + Assert.AreEqual(differentValue, instance.Value); + Assert.AreEqual(differentValue, scriptableValue.Value); + Assert.IsFalse(eventInvoked); + + void ValueChanged(TValue previousValue, TValue newValue) + { + Assert.AreEqual(MakeDifferentValue(default), previousValue); + Assert.AreEqual(MakeDifferentValue(default), newValue); + eventInvoked = true; + } + } + protected abstract TValue MakeDifferentValue(TValue value); } } \ No newline at end of file