Pre-export method to copy AssetBundles into StreamingAssets folder

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?

I can’t really help you with your problem, but I had a similar problem some time ago:

In general I think it would be very useful to be able to exclude/include assets in SteamingAssets for certain platforms. This also would work outside of could build and it would make all this work unnecessary.

Thanks for sharing the link to your similar problem; it made me realise I hadn’t wrapped the File.Copy method in a try/catch block. After adding this I found that the destination directory isn’t checked-in to Git (it was in our gitignore file) - oops! Creating the destination directory using Directory.CreateDirectory before trying to copy into it resolved my issue.

Definitely agree that it would be very useful for be able to have platform-specific assets in StreamingAssets; perhaps in a similar way to the Plugins folder with platform-specific subfolders.

1 Like

I found a feature-request for platform-specific StreamingAssets, here, in case anyone finding this thread wants to throw-in votes:
https://feedback.unity3d.com/suggestions/platform-dependent-streamingassets-folders