Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pre-export method to copy AssetBundles into StreamingAssets folder

Discussion in 'Unity Build Automation' started by StickSports_Nick, Oct 7, 2015.

  1. StickSports_Nick

    StickSports_Nick

    Joined:
    Jul 17, 2014
    Posts:
    7
    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:

    Code (CSharp):
    1. public static void OnPreExportAndroid()
    2. {
    3.     CopyAssetBundlesToStreamingAssets( "_and" );
    4. }
    5.  
    6. private static void CopyAssetBundlesToStreamingAssets( string assetBundleSuffix )
    7. {
    8.     string relativePathToStreamingAssetsFolder = "Assets/StreamingAssets/Bundles";
    9.     string relativePathToAssetBundlesFolder = "Bundles/StreamingAssets";
    10.     string absolutePathToAssetBundlesFolder = Path.Combine( Directory.GetCurrentDirectory(), relativePathToAssetBundlesFolder );
    11.    
    12.     // Copy matching AssetBundles to StreamingAssets
    13.     foreach ( string pathToAssetBundle in Directory.GetFiles( absolutePathToAssetBundlesFolder ) )
    14.     {
    15.         if ( pathToAssetBundle.EndsWith( assetBundleSuffix ) )
    16.         {
    17.             string assetBundleFilename = Path.GetFileName( pathToAssetBundle );
    18.  
    19.             Debug.Log( "Preprocess: Copying " + assetBundleFilename + " to " + relativePathToStreamingAssetsFolder + " folder" );
    20.             FileUtil.CopyFileOrDirectory( Path.Combine( relativePathToAssetBundlesFolder, assetBundleFilename ), Path.Combine( relativePathToStreamingAssetsFolder, assetBundleFilename ) );
    21.  
    22.             Debug.Log( "Preprocess: Importing " + assetBundleFilename );
    23.             AssetDatabase.ImportAsset( Path.Combine( relativePathToStreamingAssetsFolder, assetBundleFilename ) );
    24.         }
    25.     }
    26. }
    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?
     
  2. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I can't really help you with your problem, but I had a similar problem some time ago:
    http://forum.unity3d.com/threads/postprocessbuild-preprocessbuild.293616/

    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.
     
  3. StickSports_Nick

    StickSports_Nick

    Joined:
    Jul 17, 2014
    Posts:
    7
    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.
     
    xucian likes this.
  4. Malkyne

    Malkyne

    Joined:
    Nov 18, 2009
    Posts:
    39