Fix serialization of ControlReference when used in prefabs

#3262
This commit is contained in:
Wojtek Figat
2025-03-07 18:55:03 +01:00
parent 06665734e7
commit bc6f4f50cb
3 changed files with 55 additions and 14 deletions

View File

@@ -492,6 +492,16 @@ namespace FlaxEngine.Json
writer.WriteValue(JsonSerializer.GetStringID(&id));
}
/// <inheritdoc />
public override void WriteJsonDiff(JsonWriter writer, object value, object other, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is IControlReference valueRef &&
other is IControlReference otherRef &&
JsonSerializer.SceneObjectEquals(valueRef.UIControl, otherRef.UIControl))
return;
base.WriteJsonDiff(writer, value, other, serializer);
}
/// <inheritdoc />
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
@@ -499,7 +509,7 @@ namespace FlaxEngine.Json
if (reader.TokenType == JsonToken.String && result is IControlReference controlReference)
{
JsonSerializer.ParseID((string)reader.Value, out var id);
controlReference.UIControl = Object.Find<UIControl>(ref id);
controlReference.Load(Object.Find<UIControl>(ref id));
}
return result;
}

View File

@@ -228,6 +228,23 @@ namespace FlaxEngine.Json
CacheManagedOnly.Dispose();
}
/// <summary>
/// The default implementation of the values comparision function used by the serialization system.
/// </summary>
/// <param name="objA">The object a.</param>
/// <param name="objB">The object b.</param>
/// <returns>True if both objects are equal, otherwise false.</returns>
public static bool SceneObjectEquals(SceneObject objA, SceneObject objB)
{
if (objA == objB)
return true;
if (objA == null || objB == null)
return false;
if (objA.HasPrefabLink && objB.HasPrefabLink)
return objA.PrefabObjectID == objB.PrefabObjectID;
return false;
}
/// <summary>
/// The default implementation of the values comparision function used by the serialization system.
/// </summary>