I’ve been trying to make use of Unity Cloud Build’s pre-export method to copy AssetBundles for the relevant platform into the StreamingAssets folder. We want to do this so that iOS AssetBundles aren’t included in the Android build and vice versa.
We currently store our AssetBundles for all platforms outside of the Assets folder in Bundles/StreamingAssets; the filename suffix is _and for Android and _ios for iOS. Before each build we copy the relevant AssetBundles for the platform into the Assets/StreamingAssets/Bundles folder.
I’ve written the following public static method in an editor script which automates the copying of the relevant AssetBundles:
public static void OnPreExportAndroid()
{
CopyAssetBundlesToStreamingAssets( "_and" );
}
private static void CopyAssetBundlesToStreamingAssets( string assetBundleSuffix )
{
string relativePathToStreamingAssetsFolder = "Assets/StreamingAssets/Bundles";
string relativePathToAssetBundlesFolder = "Bundles/StreamingAssets";
string absolutePathToAssetBundlesFolder = Path.Combine( Directory.GetCurrentDirectory(), relativePathToAssetBundlesFolder );
// Copy matching AssetBundles to StreamingAssets
foreach ( string pathToAssetBundle in Directory.GetFiles( absolutePathToAssetBundlesFolder ) )
{
if ( pathToAssetBundle.EndsWith( assetBundleSuffix ) )
{
string assetBundleFilename = Path.GetFileName( pathToAssetBundle );
Debug.Log( "Preprocess: Copying " + assetBundleFilename + " to " + relativePathToStreamingAssetsFolder + " folder" );
FileUtil.CopyFileOrDirectory( Path.Combine( relativePathToAssetBundlesFolder, assetBundleFilename ), Path.Combine( relativePathToStreamingAssetsFolder, assetBundleFilename ) );
Debug.Log( "Preprocess: Importing " + assetBundleFilename );
AssetDatabase.ImportAsset( Path.Combine( relativePathToStreamingAssetsFolder, assetBundleFilename ) );
}
}
}
Then on the Unity Cloud Build advanced Android settings I have entered GameBuildPostprocessor.OnPreExportAndroid as the Pre-Export Method Name. The script gets run correctly but unfortunately Unity Cloud Build is failing with the following log:
Preprocess: Copying Assets_7_ios to Assets/StreamingAssets/Bundles folder
ERROR: Caught exception invoking method ‘GameBuildPostprocessor.OnPreExportAndroid’: Exception has been thrown by the target of an invocation.
I have tried both the FileUtil.CopyFileOrDirectory Unity API and File.Copy .NET API (using absolute paths) but Unity Cloud Build fails with both. Does Unity Cloud Build prevent copying files in a pre-export script?