Serialising ArrayLists

Another day, another question! Still going round in circles in my quest for a custom inspector and after battling the lack is an isNull method in C# I find myself in the present situation -

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class InteractiveNPC : MonoBehaviour {

    public ArrayList dialogueStore = new ArrayList();
    public int dialogueCount = 0;

...

Now, according to the documentation - http://unity3d.com/support/documentation/ScriptReference/SerializeField.html (SerializeField is irrelevant, however this page contains the list of serialisable objects) - Serialisable Arrays are serialisable in Unity. Lovely, so I checked that ArrayLists are serialisable and lo, MSDN - http://msdn.microsoft.com/en-us/library/system.collections.arraylist(VS.71).aspx - confirms that ArrayLists are serialisable, so in theory, I should be all good.

But then my question is, why does this happen:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

[CustomEditor(typeof(InteractiveNPC))]
public class InteractiveNPCInspector : Editor {

    SerializedObject obj;
    SerializedProperty dialogueStore;
    SerializedProperty dialogueCount;

    private ArrayList localStore = new ArrayList();

    void OnEnable() {
        obj = new SerializedObject(target);
        Debug.Log("Object: " + obj);
        //Object: UnityEditor.SerializedObject
        dialogueStore = obj.FindProperty("dialogueStore");
        Debug.Log("Store: " + dialogueStore);
        //Store:
        dialogueCount = obj.FindProperty("dialogueCount");
        Debug.Log("Count: " + dialogueCount);
        //Count: UnityEditor.SerializedProperty
    } 
     ...

Given I can retrieve the object itself, and the int property, why doesn't the ArrayList get retrieved? And also, why does it silently fail (until I try to use it..).

Answers on a postcard!

2 Answers

2

ArrayLists can't be serialized as they can hold any random object - You'll want to use List instead

As an added bonus, List will help with your code - you can't add random junk into it, only types which are the same (or subclasses) of the type of the list

If you haven't used them before, you'll need to be using System.Collections.Generic, and you declare them (and in this case instantiate) as such:

List<TypeYouWantToStore> myList = new List<TypeYouWantToStore>();

Cheers, thats a start.. How about multi dimensional lists? I.e. lists within lists? It's a bit of a pain that the documentation surrounding this aspect is quite so spotty, having come from doing Obj-C & Ruby development, I'm starting to really loathe C#.

This really isn't a c# thing, it's a unity thing. For lists within lists, the only thing I can think of is a List of a serializable class which contains a List itself

Well, I like Unity so I'd feel bad blaming it for this mess.. I was just about to come back with the lists of serialisable classes. I was trying to avoid having to create a myriad of little utility classes, but it's looking pretty unavoidable now! Many thanks regardless!

It doesn't say serializable arrays, it says "Arrays of a serializable type".

So an array of one of the serializable types mentioned therein would be serializable.

Because an ArrayList is non-typed and can hold pretty much anything, it would not be guaranteed to be an array of a serializable type and would therefore not be serializable. As Mike mentioned, a List, being a typed array would however be serializable.

Give the way in which the list of types is written, to me it suggests that "- All basic data types like int, string, float, bool. - Some built in types like Vector2, Vector3, Vector4, Quaternion, Matrix4x4, Color, Rect, Layermask.. - Arrays of a serializable type" would be Array subclasses which are themselves serialisable as indicated by the [Serialisable] flag. The documentation does not give a definitive list, but instead "Some built in types like .. ". That phrase indicates there are other unmentioned types which can be serialised - it's not very helpful at all.

Admittedly, it is ambiguous there. Where it says "some built-in types", I believe it is referring to classes and structs that are built into Unity, but do not inherit from UnityEngine.Object - Every one of them is listed in the Runtime classes section of the script docs. Where it says "all basic data types", this does not include containers. "Arrays of a serializable type" would include arrays and some derived types like List, but only with single dimensions.