- Home /
Queue method to be called after asset processor is done
Is there a way to queue a C# method to be called in the unity editor after assets has been imported?
I'm trying to build AssetBundles from within an AssetPostprocessor and get the following error:
Cannot build AssetBundles while imports of assets are in progress. Are you trying to build AssetBundles from within an AssetPostprocessor?
A related problem is executing some code after editor exits play-mode.
Any solution to these problems?
Found answer to the latter question about code executing when editor exits play-mode: http://answers.unity.com/answers/550598/view.html
Still need to be able to queue a method to be called after import of assets though.
Actually you just need to do what unity is telling you to do. Create class that inherits from AssetPostprocessor
and implement the PostProcession functions for the objects you want to handle.
For example, if you're trying to handle 3D models when they are imported. You would implement the OnPostprocess$$anonymous$$odel
function.
using UnityEngine;
using UnityEditor;
// Adds a mesh collider to each game object that contains collider in its name
public class Example : AssetPostprocessor
{
void OnPostprocess$$anonymous$$odel(GameObject g)
{
Apply(g.transform);
}
void Apply(Transform t)
{
if (t.name.ToLower().Contains("collider"))
t.gameObject.AddComponent<$$anonymous$$eshCollider>();
// Recurse
foreach (Transform child in t)
Apply(child);
}
}
Or if you're looking to do things when any object is imported you can just implement the OnPostprocessAllAssets
.
using UnityEngine;
using UnityEditor;
class $$anonymous$$yAllPostprocessor : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
foreach (string str in importedAssets)
{
Debug.Log("Reimported Asset: " + str);
}
foreach (string str in deletedAssets)
{
Debug.Log("Deleted Asset: " + str);
}
for (int i = 0; i < movedAssets.Length; i++)
{
Debug.Log("$$anonymous$$oved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
}
}
}
Hi Rob, I appreciate your answer. But the error doesn't tell me to do it in an AssetPostprocessor, it's guessing that i'm trying to do that and saying that I can't :)... which is the problem, I'm trying to do in an asset post-processor script, because I need to build the asset bundles after some assets have been processed. I kinda solved it by doing it when the user clicks on play, but there might be problems with that solutions - e.g. some edge case when the user adds some assets, never clicks on play and builds the project - then the asset bundles never gets built.
Well this is Editor code anyway, it cannot run on a cimpiled game... what exactly are you TRYING to accomplish? $$anonymous$$aybe it would be better to help with that ins$$anonymous$$d of trying to deter$$anonymous$$e how to solve the error.