AssetDatabase.ExportPackage includes "Packages/" dependencies that aren't included with Assets -> Export Package

I’m a bit confused about why AssetDatabase.ExportPackage is generating a difference UnityPackage with difference contents than I’d get if I manually exported the package via Assets → Export Package…

For reference, I’m exporting a single Scene, which has various dependencies. But the actual export is very simple, and only includes a single root asset (the scene itself).

Exporting manually, I right-click the scene, choose “Export Package…”, and I make sure that “Include Dependencies” is selected at the bottom.

When exporting via script, I’m using the following code, where the “sceneAsset” in question is the exact same Scene that I’m exporting manually:

var exportedPackageAssetList = new List<string>();
exportedPackageAssetList.Add(AssetDatabase.GetAssetPath(sceneAsset));
AssetDatabase.ExportPackage(exportedPackageAssetList.ToArray(), $"{PackageExportRoot}\\{sceneAsset.name}Package.unitypackage",
    ExportPackageOptions.IncludeDependencies);

Using these two approaches, the package sizes of the two packages are different, by about a megabyte. And when I import the package generated by AssetDatabase.ExportPackage, it has a ton of additional dependencies not found in the manually exported package. All of those additional dependencies are various “Packages”. For example:

image

When I export manually, none of those “Packages” dependencies are included. But when I export via AssetDatabase.ExportPackage, they are included.

Does anyone know why this is, or how to prevent it? It seems really messy for a package to try to include things into Unity’s Packages directories, or mess with what’s there already.

That seems to be the case. Unity is using a different code path internally that has a different behavior (the PackageExport window is using PackageUtility.BuildExportPackageItemsListWithPackageManagerWarning).

That might be worth to report a bug for. Though, as fixing this would probably involve adding a new export options flag to not break existing scripts, I’m not sure how much of priority that would be for Unity.

In the meantime, you could try calling the internal API using reflection or do the dependency collection yourself using AssetDatabase.GetDependencies, so that you can filter them before the export.

1 Like