From bbe08be462ee66b8c55594d20360b0284eef66f4 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 4 Mar 2024 18:25:57 +0100 Subject: [PATCH] Fix duplicating array values in Editor #1959 --- Source/Editor/CustomEditors/CustomEditor.cs | 16 ++-------------- .../CustomEditors/Editors/ArrayEditor.cs | 4 ++-- Source/Editor/Utilities/Utils.cs | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Source/Editor/CustomEditors/CustomEditor.cs b/Source/Editor/CustomEditors/CustomEditor.cs index ab87389de..ef6302e8e 100644 --- a/Source/Editor/CustomEditors/CustomEditor.cs +++ b/Source/Editor/CustomEditors/CustomEditor.cs @@ -747,7 +747,7 @@ namespace FlaxEditor.CustomEditors /// public void SetValueToDefault() { - SetValueCloned(Values.DefaultValue); + SetValue(Utilities.Utils.CloneValue(Values.DefaultValue)); } /// @@ -784,19 +784,7 @@ namespace FlaxEditor.CustomEditors return; } - SetValueCloned(Values.ReferenceValue); - } - - private void SetValueCloned(object value) - { - // For objects (eg. arrays) we need to clone them to prevent editing default/reference value within editor - if (value != null && !value.GetType().IsValueType) - { - var json = JsonSerializer.Serialize(value); - value = JsonSerializer.Deserialize(json, value.GetType()); - } - - SetValue(value); + SetValue(Utilities.Utils.CloneValue(Values.ReferenceValue)); } /// diff --git a/Source/Editor/CustomEditors/Editors/ArrayEditor.cs b/Source/Editor/CustomEditors/Editors/ArrayEditor.cs index 276417f96..25163febd 100644 --- a/Source/Editor/CustomEditors/Editors/ArrayEditor.cs +++ b/Source/Editor/CustomEditors/Editors/ArrayEditor.cs @@ -2,7 +2,6 @@ using System; using System.Collections; -using FlaxEditor.Scripting; using FlaxEngine; using FlaxEngine.Utilities; @@ -47,8 +46,9 @@ namespace FlaxEditor.CustomEditors.Editors if (elementType.IsValueType || NotNullItems) { // Fill new entries with the last value + var lastValue = array.GetValue(oldSize - 1); for (int i = oldSize; i < newSize; i++) - Array.Copy(array, oldSize - 1, newValues, i, 1); + newValues.SetValue(Utilities.Utils.CloneValue(lastValue), i); } else { diff --git a/Source/Editor/Utilities/Utils.cs b/Source/Editor/Utilities/Utils.cs index 54518bf9e..7e2d9f628 100644 --- a/Source/Editor/Utilities/Utils.cs +++ b/Source/Editor/Utilities/Utils.cs @@ -11,7 +11,6 @@ using System.Globalization; using System.Collections.Generic; using System.IO; using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; @@ -20,6 +19,7 @@ using FlaxEditor.GUI.Input; using FlaxEditor.GUI.Tree; using FlaxEditor.SceneGraph; using FlaxEngine; +using FlaxEngine.Json; using FlaxEngine.GUI; using FlaxEngine.Utilities; using FlaxEditor.Windows; @@ -203,6 +203,22 @@ namespace FlaxEditor.Utilities } } + /// + /// Clones the value. handles non-value types (such as arrays) that need deep value cloning. + /// + /// The source value to clone. + /// The duplicated value. + internal static object CloneValue(object value) + { + // For objects (eg. arrays) we need to clone them to prevent editing default/reference value within editor + if (value != null && (!value.GetType().IsValueType || !value.GetType().IsClass)) + { + var json = JsonSerializer.Serialize(value); + value = JsonSerializer.Deserialize(json, value.GetType()); + } + return value; + } + /// /// The colors for the keyframes used by the curve editor. ///