public class Car : MonoBehaviour
{
// many fields
}
[CustomEditor(typeof(Car))]
public class CarEditor : Editor
{
}
Is there a way to de-serialize the entire Car object itself in CarEditor or do I have to de-serialize each field in Car individually using serializedObject.FindProperty()?
It’s a nightmare if the Car class is large.
Usually the approach for custom editors is to call DrawDefaultInspector() and then add your own extra features and functionality. But yes, the more detailed and fiddly you want the editor to be, the more code you must write, generally speaking. You may also get value from an asset store package like Advanced Inspector or Odin Inspector.
I don’t have a problem exposing the fields I am after in the custom inspector, that’s working great using (Car)target. It’s that the fields in the inspector does not persist between changing scene or pressing play. I can de-serialize each field manually using serializedObject.FindProperty() but it is so much work and it creates a dependency between the Car and CarEditor that I hate.
Uhm the SerializedObject class can iterate through the SerializedProperties, that’s the main point of the class. Any SerializedProperty actually acts like an iterator that can be moved to the next field. The default inspector does exactly that. You get the first one by calling SerializedObject.GetIterator(). To advance the property you call Next or NextVisible on it. Make sure you check the return value of Next / NextVisible. When it returns false, there are no more properties. Note that when “enter children” is set to true, the iterator would go into sub objects or arrays as well. It’s literally the order you get when all fields are expanded in the inspector. The iterator goes through everything one by one.
The question is, if you want to create a custom inspector for your class, what do you actually want to change? Keep in mind that a custom inspector can not change what is serialized or not. It’s just responsible for displaying and editing the serialized data. All serializable data is automatically shown in the inspector.