Fix undo for custom serialized objects (eg. LocalizedString)

This commit is contained in:
Wojtek Figat
2021-04-27 10:14:19 +02:00
parent 8c79c21e61
commit ed4eeee647
4 changed files with 33 additions and 13 deletions

View File

@@ -4,6 +4,8 @@ using System;
using System.Collections.Generic;
using FlaxEditor.Utilities;
using FlaxEngine;
using Newtonsoft.Json;
using JsonSerializer = FlaxEngine.Json.JsonSerializer;
namespace FlaxEditor.History
{
@@ -114,6 +116,8 @@ namespace FlaxEditor.History
public object TargetInstance;
}
internal static JsonSerializerSettings JsonSettings;
// For objects that cannot be referenced in undo action like: FlaxEngine.Object or SceneGraphNode we store them in DataStorage,
// otherwise here:
private readonly object TargetInstance;
@@ -177,6 +181,24 @@ namespace FlaxEditor.History
};
}
/// <inheritdoc />
public override DataStorage Data
{
protected set
{
// Inject objects typename serialization to prevent data type mismatch when loading from saved state
var settings = JsonSettings;
if (settings == null)
{
settings = JsonSerializer.CreateDefaultSettings(false);
settings.TypeNameHandling = TypeNameHandling.All;
JsonSettings = settings;
}
_data = JsonConvert.SerializeObject(value, Formatting.Indented, settings);
//Editor.Log(_data);
}
}
/// <inheritdoc />
public override string ActionString { get; }