Skip to content

Commit c414de8

Browse files
committed
Fix serialization for XML and NewtonsoftJSON
1 parent 0822c17 commit c414de8

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

Scripts/Runtime/CollectableIndirectReference.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public abstract class CollectableIndirectReference
88
{
99
[SerializeField]
1010
protected string collectableGUID;
11-
11+
1212
[SerializeField]
1313
protected string collectionGUID;
1414
}
@@ -19,12 +19,21 @@ public abstract class CollectableIndirectReference<TObject> : CollectableIndirec
1919
{
2020
[NonSerialized]
2121
private TObject cachedRef;
22+
23+
/// <summary>
24+
/// Alternative to [XmlIgnore] [JsonIgnore]
25+
/// </summary>
26+
/// <returns>false, because of a circular reference and the SO is not serializable</returns>
27+
public bool ShouldSerializeRef() => false;
28+
2229
public TObject Ref
2330
{
2431
get
2532
{
2633
if (cachedRef != null)
34+
{
2735
return cachedRef;
36+
}
2837

2938
if (CollectionsRegistry.Instance.TryGetCollectionByGUID(collectionGUID,
3039
out ScriptableObjectCollection<TObject> collection))
@@ -41,6 +50,23 @@ public TObject Ref
4150
set => FromCollectable(value);
4251
}
4352

53+
/// <summary>
54+
/// Used for serializing, as the protected fields only work for Unity's serializer
55+
/// </summary>
56+
public string PairedGUID
57+
{
58+
get => collectionGUID + ":" + collectableGUID;
59+
set
60+
{
61+
var split = value.Split(':');
62+
if (split.Length == 2)
63+
{
64+
collectionGUID = split[0];
65+
collectableGUID = split[1];
66+
}
67+
}
68+
}
69+
4470
public CollectableIndirectReference()
4571
{
4672
}

0 commit comments

Comments
 (0)