0
\$\begingroup\$

I've written a AssetPostProcessor script that make some changes to prefabs and materials on import in some specific folders. I'm using this method to do that:

private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath) 
{
   // if the importedAsset is a prefab
   GameObject obj = (GameObject)AssetDatabase.LoadAssetAtPath(objName, typeof(GameObject));
   if (obj) {
        if (AssetDatabase.GetAssetPath(obj).Contains("Assets/Tiled2Unity/Prefabs")) { 
            // Here's some code that changes the asset
        }
    }
}

For some reason, every time I save this AssetPostProcessor is run which is something I don't want because this is lost time for something that isn't needed. What can I do to stop this?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The asset postprocessor point of existence is to be actually executed each time any asset in the project structure changes.

In order to prevent a post-processor from taking too much time while it executes, you need to change your logic in a way that rejects unneeded assets as quickly as possible. For example, in your code you could verify the path of the asset examining the objName and not go and load the entire asset. For instance:

if (objectName.Contains("Assets/Tiled2Unity/Prefabs")) {
   // Now you can load the actual asset.
}

The above check is way less expensive than loading the asset itself (which means reading it from the disk, and creating the respective Unity Object which represents it).

You may also refer to Sarprer Soher's example of an asset post-processor. He utilises some of the built-in methods in the post-processor which address specific asset types, such as:

  • void OnPreprocessTexture() - called for images
  • void OnPreprocessModel() - the one you need for working with FBX-es
  • void OnPreprocessAudio() - for sounds/music

The above methods have a post-process counterpart:

  • void OnPostprocessTexture()
  • void OnPostprocessModel()
  • void OnPostprocessAudio()
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .