Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FiveOhOne · Oct 06, 2010 at 02:27 PM · serializationcustom-inspector

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mike 3 · Oct 06, 2010 at 02:59 PM

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>();
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FiveOhOne · Oct 06, 2010 at 03:05 PM 0
Share

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#.

avatar image Mike 3 · Oct 06, 2010 at 03:28 PM 0
Share

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

avatar image FiveOhOne · Oct 06, 2010 at 03:31 PM 0
Share

Well, I like Unity so I'd feel bad bla$$anonymous$$g 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! $$anonymous$$any thanks regardless!

avatar image
1

Answer by skovacs1 · Oct 06, 2010 at 03:03 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FiveOhOne · Oct 06, 2010 at 03:13 PM 0
Share

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, $$anonymous$$atrix4x4, 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 ins$$anonymous$$d "Some built in types like .. ". That phrase indicates there are other unmentioned types which can be serialised - it's not very helpful at all.

avatar image skovacs1 · Oct 06, 2010 at 03:34 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Error while using SerializeField and HideInInspector tags 0 Answers

How to save sprite color change on custom inspector ? 0 Answers

Editable non-monobehaviour objects 1 Answer

Can I serialize data in an Editor class? 1 Answer

Using a custom editor script with a prefab instance causes my variables to reset on Play 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges