Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Changing assets during OnWillSaveAssets

Discussion in 'Editor & General Support' started by seraphki, Aug 14, 2020.

  1. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    Hello!

    I have an asset database, and I'd like to add certain assets to that database during OnWillSaveAssets. Part of that step is serializing out the databaseId to that asset. However, I'm noticing that I don't seem to be able to make any changes to the asset at that time - any changes made look to be overwritten by whatever state the asset was in before OnWillSaveAssets was called.

    I tried setting the Id value by digging out the desired behaviour, setting it directly, and using EditorUtility.SetDirty(); as well as by creating a serialized object, modifying the property, and using ApplyModifyProperties. Neither worked.

    I have a backup plan to just queue a round of modification/save to another tool after OnWillSaveAssets is complete, but I'd rather be able to just edit the asset directly. Is there a special way to tell the asset database that the asset its about to save has changed or is this not possible?
     
  2. seraphki

    seraphki

    Joined:
    Jul 20, 2016
    Posts:
    26
    One tactic I'm currently looking into is removing the asset I'm modifying from the returned path list, and then running AssetDatabase.SaveAssets() after I've changed and dirtied the asset. This seems to work out but I get a popup from the first call that informs me "Prefab not saved due to OnWillSaveAssets callback.". I can't have this popping up every time, and I thought the whole point of passing in that list to SaveAssets is so that you could remove items from it - I seem to be missing something again.

    I would still rather be able to just edit the asset before its saved during the initial saveassets call, removing it from the list and calling save again feels less clean and makes me nervous that something could go wrong and we'd miss the save completely.
     
  3. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    833
    Hi! Did you ever figure out a way to do this?