Editor, Inspector, SerializedProperty Best practices

I’ve played a little bit last month writing editor’s extensions and custom inspectors.

I know Unity can’t serialize C# properties (I think because they have side effects). Despite that, I often need to access them through property accessors methods or getter/setters. I came up to always writing specific Editor classes for custom inspector code:

public class AClass : MonoBehaviour
{
	[HideInInspector]
	[SerializeField]
	private string property;
	
        public string Property
        {
         get{return _assetPath;}
         set{
               //other stuffs/side effects
                _assetPath = value;
         }}
	
/////////////////////

[CustomEditor (typeof(AClass ))]
public class AClass Editor : Editor
{
	private AClass istance;
	
	private SerializedProperty nodePath;
	
	public void OnEnable()
	{
		istance = target as AClass;
	}
	public override void OnInspectorGUI()
	{
            Undo.SetSnapshotTarget(istance, "UndoAClass");
	    EditorGUI.BeginChangeCheck();
            string val =   EditorGUILayout.TextField("Value", istance.Property);
            
            if (EditorGUI.EndChangeCheck())
            {
                Undo.CreateSnapshot();
	        Undo.RegisterSnapshot();
                istance.Property = val;
             }
        }

Now, the above approach is a bit tedious since I have to write code like that for all classes field, but I haven’t find nothing better. My first question: is there anyway to improve the code above if I need to write a custom inspector code that need to modify a class field through C# property setter?

Some of my extension are becoming quite useful, at least for artist team that is working with them. I’m thinking about plublishing something on the AssetStore. When reading the guidelines I found the following sentences:

It’s a bit tedious handling all undos by hand like shown above, but it works. I should be compliant with that.

I’m not using Serialized Property. Here’s the second question: how can I use SerializedProperty if I need to modify serialized fields of my class through getter/setter?
I can’t do the following because I can’t handle side effects:

 EditorGUILayout.PropertyField (instance.property, new GUIContent ("Property"));

Last question: If I can’t use SerializedProperty how can I manually support prefab ovverrides? Any example?

If anyone has additional suggestions on how improve my approach is really welcome

thanks in advance

Sorry for the BUMP. Really, no answer for that?

I’m resurrecting your thread because it is line for line exactly what I need to know, and I see no reason to duplicate the question. I’m struggling to piece together exactly what all those asset store guidelines imply, and am rather confused about what approach we’re meant to use to ensure our assets fulfill those requirements. Your boldface questions mirror mine to the letter.

Attention Unity Gurus: R.S.V.P :slight_smile:

That’s sad, that nobody has answered this topic yet.
I’m basically doing the same thing and find it kinda ugly. Especially dealing with lists.

I’d suggest throwing this thread into the Scripting section, because I’m pretty sure at least Eric and Lightstriker can answer this.

In Unity 4.3 undo is done differently. The right way of doing it today is outlined on this page:

In most cases when writing editor code, it is a good idea to use serialized property. But if you can’t, you can use

But thats a very tedious way of doing it. Also please note that if you simply want to modify data on a prefab instance. Then unity will automatically figure out what has changed for you by running a diff on the modified object. The important thing is that script objects do not have automatic dirty bit tracking so you need to call this function explicitly. Builtin classes do this automatically when you change a single property.

to get started. i recommend watching the following videos:

http://video.unity3d.com/video/3699926/unite-11-intro-to-editor
http://video.unity3d.com/video/6948005/unite-2012-advanced-editor

Joachim, thanks for the detailed reply.

I’ve watched the videos you mentioned but the question of handling Properties (essentially lists) in editor scripts was always bugging me. I think I’ll have to rewatch them again.