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
6
Question by PaxNemesis · Jun 25, 2012 at 11:01 AM · c#serializedpropertyserializedobjectmulti-object editing

Using SerializedProperty on custom classes?

I have tried for a while now to get the SerializedProperty to work on my own custom classes, but have so far been unsuccessful. Basic values works fine(bool, int, string etc), but when I try to get the custom ones the challenge begins. I have tried using the objectReferenceValue on an object that extends ScriptableObject, but this for some reason does not seem to work on multiple objects.

Example: I've tried this, and it somehow seems to work with a single object, but only affect one object if multiple is selected.

 ((MyClass)swipeSound.objectReferenceValue).MyFunction();
 ((MyClass)swipeSound.objectReferenceValue).myCustomVariable = myCustomVariable;

And would it be possible to convert a SerializedProperty to the actual type without using the ScriptableObject as a base class, something like this:

 MyEnum myEnum =((MyEnum)serializedProperty);

I was wondering if anyone have any experience with the SerializedObject/SerializedProperty variables and might have any suggestions to how to overcome these challenges?

Comment
Add comment · Show 9
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 dood_legacy · Jun 26, 2012 at 07:57 AM 0
Share

I was running into a similar issue where I could only serialize primitive types, and someone suggested looking into ISerializationSurrogate and that ended up working for me. I am now able to serialize GameObject references, Vector3 etc etc. http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializationsurrogate.aspx

avatar image dood_legacy · Jun 26, 2012 at 08:06 AM 0
Share

Ah, and here is a good example of how you'd use it: http://msdn.microsoft.com/en-us/magazine/cc188950.aspx

avatar image PaxNemesis · Jun 26, 2012 at 11:18 AM 0
Share

Thanks for the information, I'll take a closer look at it and see if it's what I need. :D

avatar image dood_legacy · Jun 27, 2012 at 05:17 AM 0
Share

If needed, I can post a code example.

avatar image Bunny83 · Jun 30, 2012 at 11:00 AM 1
Share

scriptable object is a standalone class. If you want it to be saved, it need to be an asset as well. If you don't want it to be an actual assetm use "normal" serializable data classes. Unity's serialization works way different to C# serialization.

The only objects that can (truly) be serialized are unity'S buildin objects which derives from UnityEngin.Object. Pure data-classes aren't serialized the same way. If a $$anonymous$$onoBehaviour or a ScriptableObject has such a data class as member, it is serialized along with the $$anonymous$$onoBehaviour / ScriptableObject. Each of the buildin classes are serialized on their own. $$anonymous$$onoBehaviours are serialized in a prefab sinc they are attached to gameobject. Scriptable objects are on their own. If you need them as asset, save them as asset, like the Terrain for example

Show more comments

2 Replies

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

Answer by dylanfries · Jun 29, 2012 at 01:24 PM

To write an editor class that uses SerializableProperty's to access values from the inspector, the class you're trying to reference should 1) Extend ScriptableObject ( which you've done) 2) Have all its variables be individually serializable ( which most unity types are by default and custom classes can be with some work)

It is possible to have a List in a single SerializableProperty though, as long as everything in that List is itself serializable. To build an editor class that is directly associated with a non-editor class, I believe the non-editor class has to be serializable, or at least the variables you are trying to access have to be serializable. Then you can grab a reference to that class using

 SerializedObject object; 
 OnEnable(){
    object = new SerializedObject(target);  // target is 
 }

You may also need

[CustomEditor (typeof(ClassYouAreBuildingAnEditorFor))] at the top of your file (after using declarations but before your main function) - this will open up the target variable used above, which will be a reference to your ClassYouAreBuildingAnEditorFor

To access properties from this class you can use

 SerializableProperty prop = object.FindProperty("propertyName");

I think you've gotten this far on your own. As for lists, you can use List as long as T is a serializable object. This could be int's, float, etc or could be a full custom class that is serializable. It's a bit tricky to access this, but you can use

 SerializableProperty list = object.FindProperty("ListOfSerializableObjects");
 SerializableProperty listItem = list.GetArrayElementAtIndex(i)) ;

To get or set values of SerializableProperty items, you can use

 item.floatValue // for floats
 item.objectReferenceValue // for objects

Keep in mind you can iterate serialized lists (they are type IEnumerator) and if you're using basic arrays theres a number of helper functions you can call from SerializedProperty.

Theres a number of other things to do, as the objects are stored as type SerializableObject not as their original type, but I would recommend watching the Intro to Editor Scripting video from Unite 2011 Linked here as that was helpful for me, and likely more accurate. It's a little complicated but once you get the hang of which objects are which type and how to access those, its not too bad. Look at the script documentation for SerializableObject and SerializableProperty as well. Best of luck and I hope this helps.

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 PaxNemesis · Jun 29, 2012 at 02:26 PM 0
Share

Sure, I'm trying to use SerializedProperty in an editor class to store values from the base class. This in itself isn't really a problem, where my real challenge begins is when I try to access Lists/Arrays or enums or functions from a none ScriptableObject(if it's even possible).

Enums:

//How do I get the Enum value from the SerializedProperty?

$$anonymous$$yEnum myEnum = someSerializedProperty.; or someSerializedPropertyWithList.GetArrayElementAtIndex(0). none basic value/>;

$$anonymous$$aybe I've misunderstood how the SerializedProperty should be used so all help and directions would be greatly appreciated. :)

avatar image Rodolfo-Rubens · Nov 03, 2016 at 01:50 AM 0
Share

Thanks a lot! Exactly what I was looking for for ages.

avatar image
2

Answer by caLLow · May 23, 2014 at 06:54 PM

Below is a C# function call for the editor to display child properties of a serialized property.

Usage:

 public override void OnInspectorGUI() {
 
     string propertyName = "speed";
     SerializedProperty sp = serializedObject.FindProperty(propertyName);
 
     while (sp.Next(true)){
         ShowRelativeProperty(sp, "min");
         ShowRelativeProperty(sp, "max");
     }
 }
 
 // Show child property of parent serializedProperty
 void ShowRelativeProperty(SerializedProperty serializedProperty, string propertyName)
 {
     SerializedProperty property = serializedProperty.FindPropertyRelative(propertyName);
     if (property != null)
     {
         EditorGUI.indentLevel++;
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PropertyField(property, true);
         if (EditorGUI.EndChangeCheck())
             serializedObject.ApplyModifiedProperties();
         EditorGUIUtility.LookLikeControls();
         EditorGUI.indentLevel--;
     }
 }
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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set SerializedProperty of custom type to null 1 Answer

Changing Inspector's Serialized Property Label (With Code Sample) 1 Answer

How to iterate through all serialized property children ( deep search ) 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