Search Unity

Best practices documentation for custom inspectors in new prefab workflow?

Discussion in 'Prefabs' started by jwvanderbeck, May 29, 2019.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    I am migrating editor tools that rely very heavily on custom inspectors to work in 2019.1 and the new prefab workflow and I am running into , well, a lot of problems. The biggest being that changes made in our tools inside the prefab editor don't seem to be saved properly.

    Everything works fine in the old workflow, but not under the new system.

    Are there different ways we need to work in the custom inspectors and when adding/removing sub objects, or other data in the new system? Is there any documentation on this?
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    I've noticed that if I turn off Auto-Save and make these changes, the manual Save button never becomes enabled, so clearly the system isn't seeing the changes made by my script at all. Is there some new system I need to hook into?
     
  3. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,612
  4. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    So this is in addition to the standard serializedObjct.ApplyModifiedProperties() which is all that was required before?

    Every single property modification will require this call now?

    EDIT: Also that last call takes a UnityEngine.Object but not every property is a UnityEngine.Object. Does this mean Unity no longer supports regular classes?
     
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    502
    I too think this needs to be documented better. The docs say to not use SetDirty unless in a special case where we modify a non-scene asset that should not record undo. Previously, for GameObjects in scenes and prefabs it was enough to use SerializedProperties, which handled everything (so they should still do this?). If for any reason SerializedProperties can't be used, recording Undo manually would also mark the asset as dirty.

    Which of the different variants are recommended?

    By the way, I think this type of information should really be concentrated in "How To - Modifying Data with custom editors"-style articles. Currently, the information about how to work with these systems is scattered across all the different parts (CustomEditor, CustomPropertyDrawer, PrefabUtility, SerializedObject, Undo, etc). It would be much more helpful to have a single entry point that lists all the different approaches to saving data und recording data for different use-cases, such as "How to save data, record und and handle everything Unity style when making modifications to different objects" or "How to persist changes made to assets, prefabs, GameObjects without undo, when modifying from script".
     
  6. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    535
    Yes, exactly. This all worked fine under the old system, by just calling the proper serializedObject bookends for the undo system. All the properties would be recorded fine, and in fact they still are if you do the editing of the prefab in the normal scene rather than in the prefab editor.

    I can't go calling RecordPrefabInstanceModifications for each and every property modification as it happens. Not only would that make code super messy with tons of calls to it, but it only accepts a UnityEngine.Object which not every property derives from.