Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Soraphis · Mar 18, 2016 at 04:32 PM · custom editorserializedpropertyserializedobject

Serializedproperty assignment

question is at the end of the post

I have the following structure in my code.

 [Serializable]
 public class SomeClass : ScriptableObject{
     public OtherClass[] others;
 }
 
 [Serializable]
 public class OtherClass{
     public string name;
     // .... 
     public static OtherClass GenerateOtherClass(){ /* .... */ }
 }

and there is a Custom Editor for SomeClass

 [CustomEditor(typeof(SomeClass))]
 public class SomeClassEditor : UnityEditor.Editor {
     public override void OnInspectorGUI() {
         var others = serializedObject.FindProperty("others");    
         // ....
         if (GUILayout.Button("Generate")) {
             var o = OtherClass.GenerateOther();
             /** append o to 'others' **/
         }
         // ....
         sobj.ApplyModifiedProperties();
     }
 }

I want to add a new OtherClass object to the list of others. note: OtherClass is not a ScriptableObject

edit to clarify, the question is how do i get every variable/field/member of a Serializable-Object into a Serializeableproperty (structure) without listing every element manually end-edit would be cool if someone knows a solution for this.

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
1

Answer by troien · Mar 18, 2016 at 06:22 PM

You could use SerializedProperty.InsertArrayElementAtIndex. Note that this will insert an empty object to the array at given index.

 if (GUILayout.Button("Generate"))
 {
     int newIndex = others.arraySize;
     others.InsertArrayElementAtIndex(newIndex);
     /* you can get the SerializedProperty of your new object using
     "others.GetArrayElementAtIndex(newIndex);" if you need to */
 }


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 Soraphis · Mar 18, 2016 at 09:24 PM 0
Share

this is the easy part (to increase the array size). But how do i get every field of a Serializable object into a SerializedProperty structure, without listing every element manually

avatar image troien · Mar 19, 2016 at 08:38 AM 1
Share

Ah ok. Well, using the SerializedProperty there isn't a way to do that I believe (other then perhaps the more hacky like approaches of which one you used yourself)

You could also cast the Editor.target to your 'SomeClass' and add it manually through code. You do need to call Undo.RecordObject(target) before changing. (Has been a while sinse I made Editor scripts and Unity changed some things after 5.3, but I believe this still works). Note however that if you want $$anonymous$$ulti-Object editing with this, you'll need to implement that yourself...

avatar image
1

Answer by Soraphis · Mar 18, 2016 at 11:21 PM

Well i think i got it myself. Maybe theres some nicer solution. but here is mine:

  private void fillPropertyFromObject(SerializedProperty property, object o) {
             if(! o.GetType().IsSerializable) throw new ArgumentException("object is not Serializable");
 
             var it = property.Copy();
             it.NextVisible(true);
             var d = it.depth;
             do {
                 var field = o.GetType().GetField(it.name);
                 switch (it.propertyType) {
                     case SerializedPropertyType.Generic: 
                         break;
                     case SerializedPropertyType.Integer:
                         it.intValue = (int) field.GetValue(o);
                         break;
                     case SerializedPropertyType.Boolean:
                         it.boolValue = (bool)field.GetValue(o);
                         break;
                     case SerializedPropertyType.Float:
                         it.floatValue = (float)field.GetValue(o);
                         break;
                     case SerializedPropertyType.String:
                         it.stringValue = (string)field.GetValue(o);
                         break;
                     default:
                         throw new NotImplementedException();
                 }
             } while(it.NextVisible(false) && it.depth >= d);
         }
Comment
Add comment · 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

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

51 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding property with serializedObject on script with a generic 0 Answers

Calling a method on a serialized property (Custom Editor with Reorderable List) 0 Answers

Incrementing a SerializedProperty on a per-object basis 1 Answer

How to make a custom class that is serializable and works in a custom editor gui array which imitates the real inspector array? 0 Answers

How to update serializedField showed on the inspector by script? 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