Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting animation events in AssetPostprocessor

Discussion in 'Scripting' started by NoiseFloorDev, Jul 30, 2017.

  1. NoiseFloorDev

    NoiseFloorDev

    Joined:
    May 13, 2017
    Posts:
    104
    I'm trying to import animation events from metadata using AssetPostprocessor, by reading a JSON file alongside the FBX and adding them to ModelImporterClipAnimation.events.

    If I do this in OnPreprocessModel, events aren't imported on initial import. Both clipAnimations and defaultClipAnimations are always empty. I think OnPreprocessModel happens before the FBX file is even read, so there's just no data. However, it does work the second time (if the FBX changes or I manually hit reimport).

    If I do it in OnPostprocessModel, events are saved and visible in the inspector, but the old data is still used if I play the scene. If I import a second time, the previous import's events are now used (and any changes this time aren't). Doing some binary searching in my project, I see that the data in Library lags behind by one import. It looks like Unity is updating the asset database, then running OnPostprocessModel, and the changes made in OnPostprocessModel are only updated to the asset database the next time. (However, this does work the first time.)

    So, I have to run my event importing in both OnPostprocessModel (so the initial import works) and also in OnPreprocessModel (so subsequent imports work). I tried calling EditorUtility.SetDirty, but I don't think that's applicable here.

    It's a quick process, so this doesn't hurt anything, but it's strange and I'm not sure if my workaround is reliable (having imports sometimes not take effect could cause headaches down the line). Anyone else seen this?