Exclude assets already in assetbundles from Build ?

Hi,

I was previously using Resources.LoadAsync, but as I moved my project to Unity 5, it was not available anymore (that’s a pity it was very useful) so I began to use AssetBundles.

However, when i build my android game, some textures and materials (contained in an asset bundle) are included twice in my apk. Once in the assetbundle that I put in the StreamingAssets directory, and another time because my prefabs in the resources directory also contains references to these materials.

I know that by deferencing all theses materials from the prefabs it should be solved, but I think it is not very convenient. For example, the preview of a prefab without materials (all pink) is not very informative…

As I am already assigning the the materials I need on the prefab during the game, I won’t miss the textures/materials that are included twice, because I only want to use the ones inside my assetbundle.

So my question is : is there a way to force exclude some assets from build? I already tried to move the assets already contained in the assetbundle to the Editor folder before the build, but it doesn’t prevent the double inclusion.
Or maybe this feature is already planned in Unity 5?

1 Like

Anyone?

Bump

Maybe you could use the PostProcessScene attribute to dereference all these assets on build?

Put this in an editor script:

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections;

public class PostProcessBuild : MonoBehaviour {

    [PostProcessScene]
    public static void OnPostprocessScene()
    {
        if (EditorApplication.isPlayingOrWillChangePlaymode)
            return;

       // do stuff
    }

}

Thx for your answer.

However I don’t think it would really solve my problem.

The problem doesn’t come from objects located in scenes referencing assets (textures, materials, …) , but objects located in resources. Or maybe resources modified by this kind of post process are reverted after the build (just like gameobjects changes are reverted when you stop playing in the editor)?

The thing is, I know that my problem can be solved by removing all references to meshes, textures, materials on my resources and then assemble the resources them at runtime. But this solution is, on my opinion, not user-friendly at all.

What I wanted to know was if there were some kind of solution or hack, to be able to keep references to assets in my resources for prototyping, preview (resources without meshes, materials are really difficult to preview you know), etc… and not include twice these assets (asset bundle + unity build).

Like a folder called “DontIncludeAssets” for example, which prevents Unity from including any assets it contains during the build. I don’t think it would be difficult for the Unity guys to implement it…

I already do quite the same thing by moving the assets included in asset bundles in the Editor folder just before the build, but it is not sufficient.

And I think it could also be handy for assets linked to a single platform build.

I completely agree that it’s not user friendly like this. I was quite happy with Resources.LoadAsync too.

How about putting the prefabs with the connected materials in the asset bundle and not just the assets referenced by it? I know that wouldn’t make sense when you build something by manually putting prefabs into your scene, but depending on your setup it could work.

If In unity 5 there is a new function to get the asset path of an asset in a bundle without building the bundle, so if you create instances of your prefabs by code, you could use that.

1 Like

Actually when I began to use Asset Bundles I put directly the prefabs in the asset bundle, but it is very complicated to include custom scripts with asset bundles. This is understandable because asset bundles are supposed to be downloaded (although I only use them in streaming assets for the moment) so it would be complicated to resolve conflicts between the compiled version included in the build and the one included in the asset bundle if they were not identical.

I read that it is possible somehow to include a dll in the asset bundle and then “re link” the component with the compiled script using reflection but anyway reflection is not possible on iOS as far as I know…

That the reason why I started to put only assets in asset bundles.

Hope they can enhance this before the public release of Unity 5 :wink:

I just want to chime in myself and say that I would also like an easier method for excluding assets from builds, especially if they have already been packed into asset bundles.

3 Likes