Search Unity

SerializedObject and SerializedProperty

Discussion in 'Scripting' started by tomvds, Dec 22, 2009.

  1. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Does anyone have experience using the SerializedObject and SerializedProperty classes? I'd like to make a custom inspector that also exposes properties. As far as I understand from the SerializedProperty reference (http://unity3d.com/support/documentation/ScriptReference/SerializedProperty.html) I need to make a SerializedObject and obtain the properties using FindProperty(name) or GetIterator().

    Unfortunately, FindProperty() always returns null and using GetIterator() seems to corrupt things quite badly as Unity crashes as soon as you do something with the returned object.

    This stuff is barely documented and seems to have no posts about it, so it's rather difficult for me to see if I'm just misunderstanding something or if the functionality is crippled by bugs.
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Nothing good in the Unity 3 documentation, as far as I can tell. Would love some examples.
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Yeah! Unity Guys, we want more examples!:)
     
  4. pjlbyrne

    pjlbyrne

    Joined:
    Jan 18, 2011
    Posts:
    4
    I need to achieve the same end. Have you gotten any progress on this please, tomvds?
     
  5. TheOddworks

    TheOddworks

    Joined:
    Aug 20, 2009
    Posts:
    10
    Hey,

    I've been struggling with this as well, but just now managed to use it them to change the scale in lightmap value from script.

    Don't know if its any help but here it is (sets the scale in lightmap value to 0.9 for the selected object):

    Code (csharp):
    1. var so : SerializedObject = new SerializedObject(Selection.activeGameObject.GetComponent(Renderer));
    2. so.FindProperty("m_ScaleInLightmap").floatValue = 0.9;
    3. so.ApplyModifiedProperties();
    link to my question and answer on UnityAnswers

    TheOddworks
     
    Last edited: Mar 30, 2011
  6. TimB

    TimB

    Joined:
    Jan 19, 2011
    Posts:
    65
    This is a fairly old post, but I have searched for the solution without coming up with anything. I'd like to make a custom inspector that also exposes properties using SerializedObject and SerializedProperty. I can get it to work if I expose a public field, but properties do not work. Has anyone tried this, or have an example of it?

    I can access the property if I cast the editor target to the class I'm editing, but I can't do this within a serialized object.

    Thanks
     
  7. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Well, properties are not serialized by Unity, so there is no way to expose them using SerializedObject/Property. You will have to do that manually, so to say.
     
  8. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    If you put the [SerializeField] on the private backing field of the property you can have a public property and private field behind it but still have it's value modifiable in Inspector. One bad thing is modifying the value bypasses any checks or operations on the property itself.
     
  9. TimB

    TimB

    Joined:
    Jan 19, 2011
    Posts:
    65
    At that point I may as well just convert all properties to public fields and figure out another way to do checks. Sounds like that may be the way I have to go. Thanks
     
  10. Razieln64

    Razieln64

    Joined:
    May 3, 2008
    Posts:
    129
    I'm using reflection and the editorGUILayout class to set properties. It's working fine, except that you need to set the object's instance to dirty in order to save the changes. The only thing I can't do with my solution is multiple editing. Only Serialized Properties work with that.

    For that you would need to call :
    Code (csharp):
    1. EditorUtility.SetDirty(target);
    I don't know if you can use popups to set strings values in a component when using Serialized Properties. With my solution it just works.
     
    Last edited: Jan 6, 2012
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    I have been experimenting with this and found that the following undocumented functionality makes it very easy to replicate the built-in inspector window. I am not sure that it is wise to use this functionality, but would be useful:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. public class CustomWindow : EditorWindow {
    6.    
    7.     [MenuItem("Window/Test")]
    8.     public static void Test() {
    9.         EditorWindow.GetWindow<CustomWindow>("Test");
    10.     }
    11.    
    12.     GameObject activeGO;
    13.     Editor editor;
    14.    
    15.     void Update() {
    16.         if (activeGO == Selection.activeGameObject)
    17.             return;
    18.        
    19.         activeGO = Selection.activeGameObject;
    20.         editor = null;
    21.        
    22.         if (activeGO != null)
    23.             editor = UnityEditor.ActiveEditorTracker.MakeCustomEditor(activeGO.transform);
    24.        
    25.         Repaint();
    26.     }
    27.    
    28.     void OnGUI() {
    29.         GameObject go = Selection.activeGameObject;
    30.         if (go == null)
    31.             return;
    32.        
    33.         // Use the registered editor for `Transform`
    34.         EditorGUILayout.InspectorTitlebar(true, editor.target);
    35.         editor.OnInspectorGUI();
    36.     }
    37.    
    38. }
    39.  
     
  12. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
  13. gpiper

    gpiper

    Joined:
    Sep 17, 2012
    Posts:
    1
    How did you access the property this way? Would you be able to elaborate about casting the editor target? Or send me to documentation about it. Were you able to edit the property in the Unity Inspector in playmode?