SerializedObject and SerializedProperty

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 (Unity - Scripting API: SerializedProperty) 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.

Nothing good in the Unity 3 documentation, as far as I can tell. Would love some examples.

Yeah! Unity Guys, we want more examples!:slight_smile:

I need to achieve the same end. Have you gotten any progress on this please, tomvds?

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):

var so : SerializedObject = new SerializedObject(Selection.activeGameObject.GetComponent(Renderer));
so.FindProperty("m_ScaleInLightmap").floatValue = 0.9;
so.ApplyModifiedProperties();

link to my question and answer on UnityAnswers

TheOddworks

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

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.

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.

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

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 :

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.

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:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class CustomWindow : EditorWindow {
	
	[MenuItem("Window/Test")]
	public static void Test() {
		EditorWindow.GetWindow<CustomWindow>("Test");
	}
	
	GameObject activeGO;
	Editor editor;
	
	void Update() {
		if (activeGO == Selection.activeGameObject)
			return;
		
		activeGO = Selection.activeGameObject;
		editor = null;
		
		if (activeGO != null)
			editor = UnityEditor.ActiveEditorTracker.MakeCustomEditor(activeGO.transform);
		
		Repaint();
	}
	
	void OnGUI() {
		GameObject go = Selection.activeGameObject;
		if (go == null)
			return;
		
		// Use the registered editor for `Transform`
		EditorGUILayout.InspectorTitlebar(true, editor.target);
		editor.OnInspectorGUI();
	}
	
}

Please vote (http://feedback.unity3d.com/forums/15792-unity/suggestions/2948619-document-unityeditor-activeeditortracker-makecust) if you would like to see this functionality documented by the kind folks at Unity!

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?