Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Serializing the object that the Editor class is targeting?

Discussion in 'Scripting' started by MSoderberg, Mar 23, 2022.

  1. MSoderberg

    MSoderberg

    Joined:
    Jun 14, 2019
    Posts:
    20
    Lets say I have the following two classes:

    Code (CSharp):
    1. public class Car : MonoBehaviour
    2. {
    3.    // many fields
    4. }
    5.  
    6. [CustomEditor(typeof(Car))]
    7. public class CarEditor : Editor
    8. {
    9.    
    10. }
    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.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    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.
     
  3. MSoderberg

    MSoderberg

    Joined:
    Jun 14, 2019
    Posts:
    20
    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.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Are you just not marking the right things dirty, such as with Undo.RecordObject() or the editor utility SetDirty call? (I forget the API exactly)
     
    MelvMay likes this.
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    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.
     
  7. MSoderberg

    MSoderberg

    Joined:
    Jun 14, 2019
    Posts:
    20
    This was the problem. Now I run EditorUtility.SetDirty() after each change in the custom inspector and it works as intended. Thanks.
     
    Kurt-Dekker and MelvMay like this.