How do I serialize an Array of Lists of Structs? :D

[MODERATOR NOTE]: this thread contains out of date information. modern unity versions can serialize struct as long as they have the [System.Serializable] attribute. However, serialization of interface readonly struct, record, record struct, readonly record struct, Tuple and ValueTuple is still impossible within normal unity serialization.

Hello everyone,

I already spent a day trying to wrap my head around property serialization in Unity. I’m trying to create a custom inspector that lets me setup a list of actions that are carried out on specific input events.

The inspector works well and everything, but I cannot get it to survive the switch to Play mode, since I cannot figure out a way to structure the data in a way Unity can serialize.

Could anyone please have a look and give me a hint?:frowning:

public class InputListener : InputReceiver
{
    
    [System.Serializable] 
    public struct InputAction
    {
        public string actionName;
        
        public float delay;
        public bool objectState;
        public bool componentState;
        public Vector3 targetPosition;
        public string sendMessage;
        public AudioClip playAudio;
    }

    [System.Serializable]
    public class InputActions
    {
        [SerializeField]
        public List<InputAction>[] actions = new List<InputAction>[6];
    }

}

I thought that wrapping a List in a member class helps Unity serialize it. But I canno get it to work. Please help!

PS: The thread tile is wrong, it’s a List of Lists of Struct :smile: I changed that while writing this post

I tried the following, suggested in
this thread, but it does not work either:

	[System.Serializable] 
	public struct InputAction
	{
		public string actionName;
		public float delay;
		public bool objectState;
		public bool componentState;
		public Vector3 targetPosition;
		public string sendMessage;
		public AudioClip playAudio;
	}
	
	[System.Serializable]
	public class ActionListWrapper
	{
		public List<InputAction> actions = new List<InputAction>();
	}
	
	[System.Serializable]
	public class InputActions
	{
		public List<ActionListWrapper> actionLists = new List<ActionListWrapper> ();
	}

You can’t serialize structs in Unity. Making a class out of the struct would be by far the easiest workaround.

This worked, thank you very much.

But I had this working before rewriting the system to allow Lists of actions.

When I was still using a single action per Input, it worked perfectly.

Anyway, I’m happy now, thanks again

Here’s a great video about Unity serialization
Serialization in-depth with Tim Cooper: a Unite Nordic 2013 presentation

I would like an alternative answer to this. I want to use Array-of-structs because it saves me a lot of memory. It’s OK if I have to write my own serializer, but how can I have them get called when Unity un/serializes?

Then don’t rely on Unity to do your serialization. My JSON .NET serializer will serialize and deserialize an array of structs. You could also try some others like JsonObject or JsonFX.