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. Dismiss Notice

Loading a ScriptableObject in OnAssignMaterialModel

Discussion in 'Scripting' started by OwenG, Jul 28, 2015.

  1. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    Hi everyone,

    I'm running into an interesting problem. Right now I have some asset postprocessor code that runs in OnAssignMaterialModel. The first thing it does is load a ScriptableObject so that I can access some global asset data I need. I am currently using Resources.Load (as the SO lives in the Resources folder) to load the asset.

    This all works well in the editor when I change one of my model files and it reimports, or if I right-click and reimport a model asset directly. However, if I tell Unity to "reimport all", it fails. Specifically, Resources.Load returns null in this case. I've tried changing this to AssetDatabase.LoadAssetAtPath and this also works in-editor, but also returns null on "reimport all".

    I suspect it's not working because to reimport all, Unity restarts itself and the editor isn't actually running during reimport. I'm not sure why it wouldn't let me load an asset though, unless not even the AssetDatabase exists yet?

    Has anyone ever run into this? Anyone have any ideas on how to load my asset? Work arounds? I spent the day trying different things with nothing working and having to reimport our entire game each time, so any tips are appreciated. :)

    Thanks!
    Owen
     
  2. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    I realized in the shower this morning (where I do all my best coding) that I don't actually need to do the postprocessing on reimport all. If my ScriptableObject load fails, I can just bail out of my processor code early and it shouldn't be a problem. The problem with my current code is that I create a new asset if the load fails, so I just need to make sure that doesn't happen.

    So, thanks if you had a read-through of my problem, but I have my work-around. :) I'd still be curious to know why you can't load assets during a reimport all, though. Does anyone know if it's because the asset itself is slated for reimport? I'm just curious...

    Owen