For a recent project I worked on, I needed to save a list of body parts attached to a character (which was procedurally generated). I opted for using Unity’s JsonUtility and created a SerializableTransform class.
The position, rotation and scale are easy enough to save, however the problem comes in when saving the children of each part. For this, I thought it best to use a recursive function to find and save all the children of the root transform provided, however this warning pops up (during edit mode) and on saving/loading a character (during play mode):
Here is the code for the SerializableTransform.cs class:
using System;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class SerializableTransform
{
public Vector3 position;
public Vector3 scale;
public Quaternion rotation;
public List<SerializableTransform> children = new List<SerializableTransform>();
public SerializableTransform(Transform transform)
{
position = transform.position;
scale = transform.localScale;
rotation = transform.rotation;
foreach (Transform child in transform)
{
children.Add(new SerializableTransform(child));
}
}
}
I can see that there is something off, but I’m not quite sure why? The actual saving and loading functionality still works fine, but I would of course like to figure out what I’m doing wrong! Any help on why this may be happening would be really appreciated!
Thank you for your time, and I apologize in advance for my ignorance!
Unity will serialize your SerializableTransform class in UI and then it has a list of children, which also are of type SerializableTransform which also can have a list of children… and so on. Unity has set a depth limit of 7 for serialization (for infinite loop) so maybe you run into it. That is why I personally usually have tried to avoid storing hierarchical data in this format, as this rule is easy to break, but then again I haven’t done anything that complicated…
See older discussions about this topic (IIRC there was also some official blog post from several years ago):
Thanks for your reply @eses , I really do appreciate it! I’ll look into a solution using that, however I’m just not quite sure why my previous recursive implementation it is creating an infinite loop (as shown by the serialization hierarchy)?
Are you certain it’s infinite, as opposed to just being “more than 7 deep”? Unity’s serialization depth is really not very deep and there’s no identifying information on those Transforms to confirm whether they’re the same or different.
In any case, go outside of JsonUtility for this. Newtonsoft Json.NET doesn’t have a depth limit (it does actual reference comparison to protect against recursive nesting).
Wow, that’s awesome @StarManta ! I quite like using a recursive function to achieve this, so that sounds excellent! I’ve attached an example creature which I used a JSON formatter online to format. As you can see, the attached body parts seem to be serialized correctly? I should also mention, that there realistically won’t be cases where body parts would have more than 7 children - the most would be a leg with 4 “bones”. Appreciate your help!
Hi there, I tried using @StarManta 's suggestion by downloading this from the Unity Asset store, however the warning still shows? I won’t need to exceed the depth limit of 7, however as shown above, it is apparently exceeding it? Any suggestions would be appreciated!
Edit: I need to reiterate that everything works fine, but I’m still not quite sure why I’m getting a warning message? I understand Unity doesn’t support serialization past a depth of 7, however I don’t surpass that?