Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by vexe · Feb 14, 2014 at 05:57 PM · objectscriptableobjectserializedpropertyserializedobject

How to add elements to a SerializedProperty that refernces a List where T : is a Serializable 'regular' class?

Hello,

I have a SerializedProperty that is referencing a List, where Decision is just a regular class (Not a MonoBehaviour, nor a ScriptableObject) mocked with [System.Serializable]

The problem I'm facing, I can't add new decisions!

Well, the way one would add something to a list that's referenced via a SerializedProperty is to manually increase the size of the array (via arraySize), and then set the last element directly, a method to do that would be (for strings):

 public static void Add(this SerializedProperty prop, string value)
 {
     prop.arraySize++;
     prop.GetAt(prop.arraySize - 1).stringValue = value;
 }

The problem is that I can't do this to add my Decision object:

     prop.arraySize++;
     prop.GetAt(prop.arraySize - 1).objectReferenceValue = value;

Because, objectReferenceValue is a UnityEngine.Object, while Decision is a System.Object There's no way to convert from the first to the second.

There are obvious ways around this, one to make the Decision class a ScriptableObject that way it is a UnityEngine.Object, other is to just get a direct reference to the list and just add the element - but then I would have to register undo manually.

I would really love to just keep my class a normal class, and be able to add it to the list via the SerializedProperty

Any ideas?

Thanks!

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

1 Reply

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

Answer by vexe · Feb 14, 2014 at 10:22 PM

Found the solution! - I was just watching this video for the 3rd time maybe, when I noticed @29:40, he's just incrementing the arraySize to add a new entry. But then I said oh well, that's because the entry is probably a ScriptableObject, but... surprise surprise @30:19 - it's a normal class!

Couple of things to take note of:

  1. Doing a size increment like that will duplicate the last element in the array/list.

  2. If after the increment you wanted to 'directly' access the element you added, you have to do a serializedObject.ApplyModifiedProperties(); so that the change will get reflected on the actual target object.

  3. Not a horrible thing to notice, but you should put the initialization logic of your object (in my case Decision) in its constructor since you can't really get a reference to it via the SerializedProperty to call some Init method (if there's a method you'd want to call, you have to call it directly like (target as MyClass).Field.Method(); )

So yeah that's about it, arraySize++

Comment
Add comment · Show 4 · 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 instruct9r · Dec 08, 2015 at 01:29 PM 0
Share

Heya @vexe this is cool, thanks for sharing

Anyway i wonder... How can i perform List.Remove(index) with the _serializedProperty...

Because if i try to do

 _serializedProperty.DeleteArrayElementAtIndex(index)

It removes the reference of the index, but doesn't actually removes the index, but leaves it empty...

The only way i found, doing it is by moving the index to the last position and then do arraySize--:

 if (_serializedProperty.GetArrayElementAtIndex(index).objectReferenceValue != null)
     _serializedProperty.DeleteArrayElementAtIndex(index);
 
 _serializedProperty.$$anonymous$$oveArrayElement(index, _serializedProperty.arraySize -1);
 _serializedProperty.arraySize--;

I was wondering if there is another way...

avatar image vexe · Dec 08, 2015 at 07:33 PM 2
Share

@instruct9r Yeah I remember that. Another way is to call DeleteArrayElementAtIndex twice on the same index, second time's the charm! Another way is to forget about SerializedProperties altogether, and don't use them. Awful API.

avatar image ModLunar · Jun 04, 2018 at 04:54 AM 0
Share

As always, thanks for your very insightful answers! They need you on the Unity documentation $$anonymous$$m to write details like these :P

avatar image Bubsavvy · Sep 27, 2019 at 08:17 AM 0
Share

@vexe as of Unity 2019.1.9f1 this see$$anonymous$$gly does not work. :C Whenever you increment on the next call to OnGUI the property is indeed not updated. Really annoying actually unless i am missing something. I already tried prop.serializedObject.Apply$$anonymous$$odifiedProperties as well

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to iterate through all serialized property children ( deep search ) 1 Answer

Replicate "Apply" and "Revert" button functionality in ScriptableObject Editor Inspector 2 Answers

Can UnityEvent data be transferred to/from a ScriptableObject? 0 Answers

Null SerializedProperty SerializedObject upon Removal from list 1 Answer

How do you actually use CanEditMultipleObjects 1 Answer


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