- Home /
Running AssetDatabase.ImportPackage on multiple packages
I'm trying to make a script which takes an array of *.unitypackage files and runs AssetDatabase.ImportPackage on each one. It seems that the script is running so fast that Unity only imports the past package in the list. Has anyone else ran into this issue and / or found a workaround?
Here's the code snippit I'm working with:
private static void ImportPackages()
{
//go through the entire resources directory
string resourcePath = Application.dataPath + "/Resources";
DirectoryInfo directory = new DirectoryInfo(resourcePath);
//find all *.unitypackage files
FileInfo[] unityPackageFileArray = directory.GetFiles("*.unitypackage", SearchOption.AllDirectories);
for (int i = 0; i < unityPackageFileArray.Length; ++i)
{
//pared down filename starting from the Assets/ directory
string localFilePath = unityPackageFileArray[i].FullName;
int assetIndex = localFilePath.IndexOf("Assets\\");
if (assetIndex < 0)
{
assetIndex = 0;
}
localFilePath = localFilePath.Substring(assetIndex, localFilePath.Length - assetIndex);
localFilePath = localFilePath.Replace('\\', '/');
Logger.Log("importing file: " + localFilePath);
//import them into the project
AssetDatabase.ImportPackage(localFilePath, true);
AssetDatabase.Refresh();
}
}
Thanks, ~Matt
Yes I am, but even when I change it to true, it doesn't stop for each unitypackage. $$anonymous$$aybe this is a unity bug?
$$anonymous$$aybe add the script you use to your question. I would expect iterating over your array and calling ImportPackage on each one would work.
I just edited the original post to contain the code snippit I'm working with. Thanks guys.
i just downloaded the latest version of the unity editor (3.4.0f5) and i reran this code. i am still getting the same error. any other thoughts on why this is happening? has anyone else had the chance to run my original code and verify this functionality is broken?
thanks all!
Answer by ADI · Jan 17, 2012 at 03:15 AM
I'd contacted Unity Support on this question, for a similar implementation using ImportPackage. The behavior we'd seen was the same - the import only occurs for the last package in the set. There is no workaround. Support confirmed this after we had provided examples of our attempts. ImportPackage doesn't work when called in close succession. Subsequent instances abort prior ones leaving only the last able to complete.
I'm having the exact same problem. Does anyone know how to successfully import multiple packages from code/script ?
Answer by zd · Jul 11, 2013 at 12:29 AM
I was able to get this working by instantiating an EditorWindow and using the Update function to spread out my ImportPackage calls. I had to put in a delay of about 10 frames between each call. You can't call ImportPackage on every update. I added the file paths to a list of strings.
void Update()
{
if (packagePaths.Count > 0)
{
// wait for delay. Can't import multiple packages in close succession
if (delayFrames > 0)
{
delayFrames--;
return;
}
ProcessPackage(packagePaths[0]);
packagePaths.RemoveAt(0);
delayFrames = 10;
return;
}
}
Thank you, it does work, you're a life saver !
However this obviously won't work if you want to use interactive import (second argument of ImportPackage).
Glad to help. FYI, I ended up putting a delay between the AssetDatabase.ImportPackage and AssetDatabase.Refresh calls as well.
Answer by avee · Jul 26, 2013 at 12:03 PM
This has been fixed in Unity 4.2, but still it seems that the actual import happens after all those successive ImportPackage calls. So if for instance you have a code like this:
foreach(var path in packagePaths) {
AssetDatabase.ImportPackage(path, false);
someClass.prefab = (GameObject)Resources.LoadAssetAtPath(assetPath, typeof(GameObject));
}
Is not going to work, someClass.prefab will be == null since the packages will actually be imported after all of those ImportPackage calls. Probably at the end of the editor's update frame.
Your answer
Follow this Question
Related Questions
How to make a new path register in the AssetDatabase? 2 Answers
import package with script 1 Answer
Generated sprite asset cannot be tinkered like an image in editor 0 Answers
AssetDatabase.LoadAssetAtPath is null? Any way to load Sprite asset? 1 Answer
EditorSceneManager.OpenScene fails after AssetDatabase.Refresh 1 Answer