Asset Bundles for iOS --> Can GameObjects preserve Scripts?

I have created an asset bundle for iOS and have been able to load it and initially pull a Texture2D out of it and instantiate it.

However, when I try to load a GameObject that had a script attached to it, is produces and error telling me that the script is missing.

The scripts exists in the main app and it exists in the project I did the asset bundle build from. I have not found any definitive word on whether Unity can preserve GameObjects with scripts attached to them when creating an asset bundle and reproduce these when instantiated.

Ie if I had a cube with some kind of script attached to it, and then I make an asset bundle out of the cube, when I load the asset bundle and instantiate the cube, will it still contain/reference the script I had attached to it.

Will using the BuildAssetBundleOptions.CollectDependencies option when creating the asset bundle, have any effect?

Do all GameObjects loose all their scripts and script references when saved into asset bundles?

So for any of you out there wanting the answer if you can preserve the scripts on your prefabs, the answer is yes.

The tricks is you’ll need to build with BuildAssetBundleOptions.CollectDependencies instead of BuildAssetBundleOptions.CompleteAssets. You’ll also need to make sure that this Scripts live/exist in the level or main app that the asset bundle will load in. While you can’t directly save scripts into a bundle, I believe what happens is that the scripts references get saved and because they exist in the level they are loading into, the sync up again and you’re good to go.

I’d recommend that you make a duplicate of your Unity project when you wan’t to do Asset Bundle builds and create them from this duplicate copy. I had a weird situation where doing an asset build from the main project made Unity revert from the iOS platform to something else and I got weird errors about it not being able to find .Net libraries.

You’ll need

  1. Here is my build script for unity iOS bundle deployment. You’ll need Unity Pro. Create a .js script in a folder in your project called EportBundle.js.

  2. Select the assets and prefabs you want to go into your bundle in your Project window in the editor.

  3. The script should show up at the button of the Assets menu in the main menu bar.

  4. Select the menu option and this should build a nice little bundle ready to rock under iOS.

  5. FInally, as far as I can see, an iOS targeted bundle will only work on iOS, so if you try and load it in a regular Unity project it will most likely fail.

@MenuItem (“Assets/Build AssetBundle From Selection - Track dependencies”)
static function ExportBundle(){

        var str : String = EditorUtility.SaveFilePanel("Save Bundle...", Application.dataPath, Selection.activeObject.name, "assetbundle");
        if (str.Length != 0){
        	 BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, str, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone);
        }
}