Search Unity

Editor PostProcessModel with custom settings

Discussion in 'Editor & General Support' started by Noisecrime, Apr 17, 2021.

  1. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Hi,

    So i've got a project where I need to apply custom vertex colors to specific fbx models. I figured using the AssetPostProcessor would be a good place to do this, using a simple tag on the fbx file names to run my custom vertex color generation code.

    This all works great, however I also needed to read in some specific settings for the vertex color generation code and save out some information once applied to the Unity model data. So I used a scriptableObject to supply and store this information using Resources.Load() to import it within OnPostprocessModel. Once again worked great.

    The problem starts when I delete the project Library or share the project without the library. At this point the postprocessor fails as the scriptableObject is null. As far as I can tell it appears the AssetPostprocessor runs on the fbx models before Unity processes the scriptableObject asset and so neither the Resources.Load nor the AssetDatabase is aware of it.

    I've contemplated how I might address this, trying to ensure the scriptableObject asset is loaded prior to AssetPostProcess but can't think of any method in Unity that would allow that.

    So my question is does anyone have any ideas how to make this work?
    Is there a different approach I can use?
    Does anyone have an existing solution that works?


    Essentially I need to be able to read some global level settings which I can apply in OnPostProcessModel and ideally store some results. I really liked using a scriptableObject to store these settings as they were easy to tweak in Unity editor via the inspector.

    I don't want to use Editor.preferences as I'm pretty sure they are not stored with the project.
    I don't want to hardcode these settings into the OnPostProcessModel class as it doesn't make sense to do so, since they can change dramatically depending on the projects requirements of the vertex colors i'm using.

    My best alternative at present is to maybe store this data as json and read/save it from OnPostProcessModel. Its not ideal and I'm worried if model importing is multi-threaded or becomes multi-threaded read/writing to a json file will obviously fall apart. I guess if I only read from a global json for default settings and only write to per model instances of json files that would get around any multi-threaded conflicts.

    I don't know I just feel like I'm missing something obvious here that my goggling has failed to turn up.