- Home /
Recursive multiple unity package import and asset bundle build that works in unity 5.6, 2017 and 2018.1.6 does not work in unity 2018.2.X
Hello, I'm working on an editor automation script, that allows import multiple unity packages and then builds android asset bundles of all. I'm doing this by importing one unity package and then building asset bundle available in the unity package and then repeating this recursively for all. This script is made for upgrading our unity packages of asset bundles to the desired unity version. Automation script is below:
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class UnityPackageHelper
{
static int successCount = 0;
static string[] files;
static string android_ab_path = Application.dataPath + "/android";
#if UNITY_EDITOR
[MenuItem("Tools/Unity Packages/Import Folder")]
public static void ImportPackagesFromFolder()
{
string pPath = "";
successCount = 0;
pPath = EditorUtility.OpenFolderPanel("Select Package Path", Path.GetDirectoryName(Application.dataPath), "");
if (!string.IsNullOrEmpty(pPath))
{
files = Directory.GetFiles(pPath, "*.unitypackage");
if (files != null && files.Length > 0)
{
AssetDatabase.importPackageCompleted += PackageImportSuccess;
AssetDatabase.importPackageFailed += PackageImportFail;
Directory.CreateDirectory(android_ab_path);
PackageImport();
}
}
}
#endif
static void PackageImport()
{
if (successCount == files.Length)
{
AssetDatabase.importPackageCompleted -= PackageImportSuccess;
AssetDatabase.importPackageFailed -= PackageImportFail;
Debug.Log("Import complete. " + successCount + " of " + files.Length + " packages successfully imported.");
}
else
{
string packagePath = files[successCount];
AssetDatabase.ImportPackage(packagePath, false);
}
}
static void PackageImportSuccess(string packageName)
{
successCount++;
Debug.Log("Completed import of package '" + packageName + "'");
BuildAssetBundle();
}
static void BuildAssetBundle()
{
string abname = AssetDatabase.GetAllAssetBundleNames().Except(
AssetDatabase.GetUnusedAssetBundleNames()).ToArray()[0];
BuildPipeline.BuildAssetBundles(android_ab_path, BuildAssetBundleOptions.None, BuildTarget.Android);
AssetDatabase.DeleteAsset(GetAssetBundlePrefabPath(abname));
PackageImport();
}
public static string GetAssetBundlePrefabPath(string assetBundleName)
{
foreach (var assetGuid in AssetDatabase.FindAssets(""))
{
string assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
string bundleName = AssetImporter.GetAtPath(assetPath).assetBundleName;
if (assetBundleName.Equals(bundleName))
{
return assetPath;
}
}
return "";
}
static void PackageImportFail(string packageName, string errorMessage)
{
Debug.Log("Failed to import package '" + packageName + "' with error message '" + errorMessage + "'.");
}
}
The script is written in Unity version 5.6.3 and all unity packages exported from Unity 5.6.3. It is working properly in Unity 5.6.3, Unity 2017 and Unity 2018.1.6. But it not working properly in Unity version 2018.2.X.
The issue is the first unity package gets imported and build successfully but the next unity package gets imported but does not go to the build step.
In my script, I have put the build asset bundle code in AssetDatabase.importPackageCompleted callback. The first time the callback gets called and the build executes, but the second time it does not get called and my automation script stops.
Now I am not able to understand when I am using basic unity editor APIs and the same script works in unity 5.6, 2017 and 2018.1.6, why doesn't it work in unity 2018.2.X? Are the base APIs changed in unity 2018.2.X?
I have shared the script and sample unity packages for testing and debugging. Guys, please help I am stuck with this issue since past 1 week.
Thanks in advance.
And you're not getting a PackageImportFailed either?
Is the console showing anything at all?
If you set break points, does it not return at any point at all?
Hey Hexagonius, Thanks for your reply and apologized for the delay. I'm not getting either importPackageFailed or importPackageCompleted. Nothing is showing in the console and I put breakpoint PackageImportSuccess, but it doesn't work. You can try to create some primitive object exports from Unity 5 then import above script in 2018.2 and choose the folder by click $$anonymous$$enuItem.
Hope to see you soon. Thanks!