Loading assets from assetbundles makes assets appear pink in editor.

Hello,

so as the title states, when I load an asset from a assetbundle, it loads but shows as pink in editor. On test devices it works fine. The curious thing is that it loads the shader and materials, this appears to happen only when I instantiate the asset that is loaded from assetbundles. This seems to happen for 3D meshes and particle systems, but not UI components.

Here is the code I’m using for instantiating

 var instGo = Instantiate((GameObject)AssetBundlesManager.Instance.LoadAssetFromBundle(Constants.kosuninHouseBundle, go.Key+"Female"));

go.Key is a string, with the asset name.

And loading from assetbundle:

http://unity3d.com/learn/tutorials/modules/intermediate/live-training-archive/unity5-asset-bundles :slight_smile:

Am I not supposed to straight load and instantiate, if so then how is supposed to be done, and why does it work on mobile ? I’m using unity 5.0.2f1, if any more info is required, I will provide them asap.

-Ali

Pink normally means a shader is missing. Where do they come from?

I’m loading them locally.

As you can see in the screenshot, the shader is present if I’m not mistaken. However reapplying the shader does fix the issue, so I’m confused :).

-Ali

Let’s back up a square. So, it looks like you have a problem in the editor (so might not be an asset bundle issue). The skinned mesh renderer has a shader which is showing its material as pink in the inspector. Is there anything in the Editor.log to help explain all this? (I wonder if the shader is actually offloaded into an asset bundle, so it’s available to the editor. If you reapply, maybe Unity sees the shader is missing, so re-loads it.)

Nothing about the mesh or shader in the logs, only that they are loaded from assetbundles succesfully. I noticed however that this doesn’t happen on my collages Windows Laptop, but it happens on my Mac. For him some of the UI elements don’t load properly, but meshes and particle systems work just fine. This is all in the editor.

So, you have an asset bundle that contains a mesh, a material and a shader. (So, everything you need to do display the model.) Does this asset bundle load and work correctly on a PC but not a Mac? What platform was it built on?

They are in different bundles, the mesh, material and shader, but that shouldn’t be an issue. On the mac I built for android, and on the PC it built for PC. Both machines using 5.0.2f1. I have tried to add that specific shader to Unitys always include list, but it didn’t work.

Also have the same bug, among a bunch of others for Asset Bundles. You can find it in bug report 697764 with a repro project as well. I am using Mac, and don’t have a Windows machine to test if it is Mac only for us too, but we have resolved it using a hack script to re-apply the shader for the materials affected, as mentioned above.

That would have been my last resort as well, but I hope this can get fixed internally. Other than this bug, assetbundles have been working very well, I hadn’t noticed any other major bugs, what did bugs did you find Kyle?

I’ve seen this problem when trying to load iOS bundles in editor on OSX. I think it is a problem with loading bundles for the wrong target (eg, mobile bundles on desktop).

Wrote up a big post about it here.

Biggest bug was not being able to put more than 1 scene in an asset bundle, and biggest issue was losing the power of the old asset bundle dependency system in place of a new system that is less automated.

This issue is rather curious as some of my assetbundles load fine on both Mac and Windows. Additionally on Mac, I’m using variants for those materials displayed in the screenshot provided above, and the house texture/shader/materials load fine, but the players don’t. The house uses the default mobile unlit shader, so that might be the cause that makes it work, but I have added the shader I use for the player into the always include list, so I’m unsure.

Ive heard this issue should be fixed in an upcoming patch release. Yay! :slight_smile:

Hello guys,

I decided to revive this thread, rather than start a new one since nothing really has changed in my project, other than the upgrade to 5.0.3. I tried 5.1, but there were constant crashes when loading assetbundles. So we decided to stick with 5.0.3 for now, I was wondering if there are any news on this or fixes ?

Thanks for your time.

EDIT:

I forgot to mention, that I made this semy hacky solution using Shader.Find to reapply the shader, also if someone is to use this solution, remember to put the shader in your resources folder.

using UnityEngine;
using System.Collections;


public class ReApplyShaders : MonoBehaviour
{
    public Renderer[] renderers;
    public Material[] materials;
    public string[] shaders;

    void Awake()
    {
        renderers = GetComponentsInChildren<Renderer>();
    }

    void Start ()
    {
        foreach(var rend in renderers)
        {
            materials = rend.sharedMaterials; 
            shaders =  new string[materials.Length];

            for( int i = 0; i < materials.Length; i++)
            {
                shaders[i] = materials[i].shader.name;
            }         

            for( int i = 0; i < materials.Length; i++)
            {
                materials[i].shader = Shader.Find(shaders[i]); 
            }
        }
    }
}
3 Likes

Can you give more details? Or could you please file a bug with repro project? Crash like this is something we should fix.

It seems to have gotten fixed, I’m currently using 5.1.1p2 and everything is fine.

I’m not sure if the shader issue has been fixed yet, but the script I posted above works fine, I have tried adding the shaders to assetbundles and as “Always Included Shaders” via the Graphics menu but that hasn’t worked.

-Ali

I’ve deal with this for years literally. Currently I just know that shader is precompiled in the assetbundle. so the one you loaded from your bundle is actually another shader though it have exactly the same name with the one in your project.

So if you see pink material in your editor, don’t be panic, it’s just that the shader in your bundle is compiled to another platform which is incompatible with your standalone platform editor. Using the reset shader code above will fix that. BTW, the legacy fix-function shader will work no matter what target platform your assetbundle is. I’m guessing it’s because there no real shader code to be compiled.

Still, there’s a chance where on the target platform app, the shader is still missing

4 Likes

Wait, so, this is intended?

We used to be able to exclude dependencies in 4.x, so we knew exactly what was in our bundles.

Now in 5.x, dependencies are always collected. Why? Why can’t we disable this? I no longer feel in control of what goes in to my bundle.

This is causing some real issues with shaders…! They turn pink, because, as mentioned above, the shader is precompiled for the target platform and included in the bundle (whether you like it or not!). This means that if I am testing in the Editor and have my platgorm set to Android, any asset bundle I load in the Editor will contain pink materials.

In 4.x, I used to exclude shaders from my asset bundles. My shaders were included in the resources folder and I was able to make the assumption that it would always be there. This way, I could exclude the shader from my bundle. When I loaded the bundle, it would use the shader already included in the application (and so it would always be the correct shader, since the shader was already compiled for the current platform whether it be windows, osx, ios or Android).

We can’t do this anymore. We need to build our asset bundles for all 3 platforms. Windows/OSX, iOS and Android.

This raises other questions too - for example, since the shader inlcuded in the bundle isn’t the exact same shader as the one in my project (it is a precompiled copy), does this break batching? Imagine if I had a custom diffuse shader which I used on all of my assets, and I had 10 asset bundles all containing different meshes/prefabs with materials that used this custom diffuse shader… Will this break dynamic batching since each asset bundle has its own copy of the same shader??

What about if I had a texture in my project that all of these prefabs were referencing? Forced dependency collection means that this texture will be copied 10 times (once for each bundle). So if I load and instantiate the prefabs in all 10 of these bundles, is this texture loaded into memory 10 times? (10x copies of the same texture).

Creating asset bundles which reference assets which are garunteed to exist in the project isn’t such a crazy idea either… Imagine a car game where each car was an asset bundle, and each car used the same shader or made use of some common texture. Or a card game like hearthstone where each card or theme of cards were an asset bundle and shared assets like the Mesh for the 3d card itself or made use of a common texture like the card backings… The forced dependency collection is just going to double up on all these assets…?

@Graham-Dunnett could you shed some light on this?

Or @

Bump