Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Loading assets from assetbundles makes assets appear pink in editor.

Discussion in 'Editor & General Support' started by MarimoAli, May 18, 2015.

  1. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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

    Code (CSharp):
    1.  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 :)

    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
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    Pink normally means a shader is missing. Where do they come from?
     
  3. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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
     

    Attached Files:

  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    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.)
     
  5. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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.
     
    Last edited: May 18, 2015
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,288
    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?
     
  7. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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.
     
  8. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    874
    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.
     
  9. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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?
     
  10. Anozireth

    Anozireth

    Joined:
    Feb 7, 2013
    Posts:
    71
    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).
     
  11. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    874
    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.
     
  12. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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.
     
  13. BFS-Kyle

    BFS-Kyle

    Joined:
    Jun 12, 2013
    Posts:
    874
    Ive heard this issue should be fixed in an upcoming patch release. Yay! :)
     
  14. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class ReApplyShaders : MonoBehaviour
    6. {
    7.     public Renderer[] renderers;
    8.     public Material[] materials;
    9.     public string[] shaders;
    10.  
    11.     void Awake()
    12.     {
    13.         renderers = GetComponentsInChildren<Renderer>();
    14.     }
    15.  
    16.     void Start ()
    17.     {
    18.         foreach(var rend in renderers)
    19.         {
    20.             materials = rend.sharedMaterials;
    21.             shaders =  new string[materials.Length];
    22.  
    23.             for( int i = 0; i < materials.Length; i++)
    24.             {
    25.                 shaders[i] = materials[i].shader.name;
    26.             }        
    27.  
    28.             for( int i = 0; i < materials.Length; i++)
    29.             {
    30.                 materials[i].shader = Shader.Find(shaders[i]);
    31.             }
    32.         }
    33.     }
    34. }
     
    Last edited: Jun 18, 2015
    jeff-smith likes this.
  15. Vincent-Zhang

    Vincent-Zhang

    Unity Technologies

    Joined:
    Jan 23, 2013
    Posts:
    106
    Can you give more details? Or could you please file a bug with repro project? Crash like this is something we should fix.

     
  16. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    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
     
  17. Sparrowfc

    Sparrowfc

    Joined:
    Jan 31, 2013
    Posts:
    100
    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
     
    Michael_HOS and MarimoAli like this.
  18. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    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?
     
    Last edited: Aug 29, 2015
  19. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    Last edited: Aug 29, 2015
  20. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
  21. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    383
    I've also experienced this problem on OSX. We've determined it is from loading shaders compiled for iOS from within OSX. Using the Shader.Find hack that @MarimoAli posted above does fix it, and it works properly on device without the shader hack.

    The problem is I have an additional issue that the materials are also losing the reference to their textures, so that I not only have to reset the shaders on materials, I also have to reassign the textures on materials.

    THIS IS REALLY BAD.

    Resetting the shaders is easy, because the materials still have a reference to them, and you can just do Shader.Find with the name.

    But the textures is a big problem, because the materials are just showing Null in the MainTexture property.

    We're currently working around this by having each material share the name with it's texture... but this is not an acceptable workaround. There are many cases when multiple materials might share a texture, and they will need to have different names.

    I've already posted about this in the forums, but I'm putting it here because @Vincent Zhang has replied at least once, and I know he works on Asset Bundles.

    Please take a look at this Vincent, this is a huge problem on my project right now.

    The case number is Case 723031
     
  22. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    I would like to add that is really is not "ok"to have to find the shader. When you load the asset bundle, it loads the shader with it (asset bundle copy). If you do Shader.Find, you are loading it a second time (project copy). This doubles up on memory usage...

    Not such a big deal with shaders... But if you use a technique like this to " fix" textures, you also double up on textures! If you have 5 asset bundles that all have the same texture in the bundle, the texture is loaded 5 times... No good.

    Same applies for meshes, materials, physic materials....etc.. Everything doubles up. Everything becomes a copy of the source!
     
  23. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
  24. FundaySoftware

    FundaySoftware

    Joined:
    Sep 16, 2014
    Posts:
    22
    Bump, we just ran into this issue as well - I doesnt really matter if the shaders run fine on device. We are now unable to keep a proper production workflow if we cant see anything in the editor but pink. Come on, guys. Any word on this? a simple acknowledgement would do.
     
  25. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    @FundaySoftware - Assuming you are working on Windows targeting Android/iOS, you can work around the pink issue by running your editor in OpenGL (since the asset bundles you've built contain precompiled shaders that only work on OpenGL....because... as I mentioned in my rant above.. Unity makes a copy of everything and reprocess it for the target system whether you want to include it in the bundle or not (@Graham Dunnett / @Vincent Zhang ))
    • Player Settings > PC, Mac and Linxu Settings > under 'Other Settings'
    • Uncheck 'automatic graphics API'.
    • Add 'OpenGLES3' as a graphics API.
    • Move it to the top of the list.
    • This will force your editor to operate under in OpenGLES3 (Your editor will probably crash when attempting to swap to OpenGL - just reload unity if it crashes and it'll be run fine after that.).
    • You can see in the window title that it is running in OpenGL (it'll say '<OpenGL core profile>').
     
    koras_dor, tiz777, Wanderer13 and 7 others like this.
  26. FundaySoftware

    FundaySoftware

    Joined:
    Sep 16, 2014
    Posts:
    22
    @Prodigga Thanks for the tip!

    I implemented the shader "hack" solution of reloading them all with the script above. We are a rather large team with both Windows and OSX users targeting both iOS and Android. Does your fix apply to OSX as well?
     
  27. Heath Farrow

    Heath Farrow

    Joined:
    Sep 3, 2015
    Posts:
    1
    On Aug 27 2015, Unity announced plans to drop support for precompiled shaders in 5.3 (2015 December). Presumably the issue outlined here could be resolved by that?
     
  28. MarimoAli

    MarimoAli

    Joined:
    Jan 16, 2015
    Posts:
    18
    Hey guys,

    I wanna hear your opinions on what can cause this and possibly on how to fix this, we had been using a custom unlit shader, but we switched to a shader with rim lights and other features(Toony Shader Pro 2 from asset store).

    When trying it out in editor, everything looks fine: http://gfycat.com/PartialAnyCopperhead

    But when loading from bundle it looks like this: http://gfycat.com/WarySaneHoneyeater

    That's when reapplying the shader, when I don't use the reapply method, it loads the base diffuse shader provided by unity.

    -Ali
     
  29. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    219
    We didn't have this issue with the 5.1.3 version, but since we switched to 5.2, it seems the bug is back in. Pink materials in the editor all around.
    The hack with reapplying the shaders seems to work.

    But i'd rather have a proper solution than a hack.
     
  30. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    Yeah, but the Unity devs don't seem to want to chime in. I've been trying for 2 weeks to get a response on the forums. We should be able to exclude dependancies from bundles and have the renderers, materials, mesh filters, etc find and use the appropriate asset from within the project when the bundle is loaded.
     
  31. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    122
    I have the same problem! Any news about that?
     
  32. holliebuckets

    holliebuckets

    Moderator

    Joined:
    Oct 23, 2014
    Posts:
    496
    Hi folks! I wanted to let you know the bug is listed as a "must fix" for 5.3 :) That doesn't give you an immediate resolution, but I want you to know it is a high priority issue. <3

    cc'ing @karl.jones so there are more than one set of eyes here ^_^
     
    karl_jones likes this.
  33. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,568
  34. polytropoi

    polytropoi

    Joined:
    Aug 16, 2006
    Posts:
    677
    upvoted! This is an annoying bug; I thought I was Doing It Wrong
     
  35. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    983
    Upvoted, major issue for us in Verdun.
    As soon as a shader is marked in a 5.X bundle, all objects (which are in seperate bundle) will show up pink.
    The only workaround is NOT marking the shader in a bundle, but this costs us 2mb overhead per assetbundle(x200). And let's hope unity is smart enough to NOT parse the shader every time..im very afraid it will, like in 5.2
     
  36. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    I am also having this issue... i tried the Shader.Find approach and it is not helping.. Mine is doing it in the editor.. And the object shows the right shader with the right texture, just pink..


    Running on windows, target is windows.. Tried compiling and running, same issue..
     
  37. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    @karl.jones are you able to confirm what I am observing is correct? All assets considered a dependancy by the asset bundle system is essentially duplicated, processed for the target platform and placed into the bundle.

    For example, if you have a generic texture that is used in other parts of your game (ie: A generic blob shadow texture), and you load 5 different asset bundles which all contain some object that makes use of this blob shadow texture, I will have 5 identicle (yet 'different') copies of the exact same blob shadow texture loaded into memory? Each asset bundle using its own 'copy' of the source? This is what I am observing in the memory profiler..

    It seems every asset that is a dependancy receives this treatment. Materials, Physics Materials, Shaders, Textures, Meshes. If you segment your game content into dozens of bundles, you are creating dozens of copies of those common assets that are supposed to be loaded once and shared between objects. I havn't tested this but it should also break dynamic batching between assets in different bundles - they might share the same material and shader within your project, but once built into a bundle they will have seperate copies of the material/shader, and so technically do not qualify for batching.

    Please shed some light on this issue.. This new system is great for beginners and simple games, which is awesome! Asset bundles are really powerful and making it more accessible is something worth striving for. I beleive the new system does it perfectly, as you dont need to put as much thought into a lot of it - the system is clever enough to take care of most of the problems with the new dependancy collection. But there needs to be a toggle to disable this dependancy collection for users who know what they are doing.. If you do not want to do that, atleast give us a way to flag certain assets as 'shared assets' which are forced to be included in the project. This way, these assets are garunteed to 'be there' when the game is built and the dependancy collection system will not need to include a copy of those assets in the asset bundle. (Similar to do Resources folder - maybe even use the Resources folder for this purpose?)
     
  38. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,568
  39. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    Hey @karl.jones thanks for the reply man!

    That documentation is outdated. It wants me to push and pop asset dependencies! Those features are depricated in Unity 5's new asset bundle system.

    In the new Unity asset bundle system, we don't build the bundles manually. You just tag which asset bundle an asset belongs to via the inspector (bottom of the inspector when you select an asset in your project) and Unity automatigically gathers dependencies for each bundle, cloning assets where assets overlap.

    There doesn't appear to be any type of "asset bundle dependancy layering". Asset bundles are just 1 dimensional in the new asset bundle system.

    The problems I've outline in my post above (redundant asset cloning, etc) are mentioned in that bit of documentation you've linked to. This is great because it validates a lot of stuff I've been seeing and shows these are very real problems..

    So, back when we had the old asset bundle system, that documentation suggests two solutions. Either:

    1) "Layer" your asset bundles by using push/pop and manually maintaining dependencies. A crude example: You could have one asset bundles with all of your shared assets and then you could push on top of that all of your individual bundles that use assets from this common pool of assets.

    Or 2) (the method I've mentioned in my first post here) Simply disable dependancy collection and leave out shared assets from the bundles. The bundles will build with those assets missing, but you as the developer know your project will contain those assets anyway (shared shaders, textures, etc maybe they are in the Resources folder, forced to be included in the project) and you will have full control over the bundles.

    The new asset bundle system does not let us do either of those!

    Push/Pop methods are obsolete
    And
    CollectDependancies flag is obsolete.
     
    Last edited: Nov 28, 2015
  40. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    219
    It is possible what you're trying to achieve, but the documentation on this was far from clear. It took me quite a while to figure out the correct way.

    This page was the one that put me on the correct track: http://unity3d.com/learn/tutorials/...undles-and-assetbundle-manager?playlist=17117. Analyzing the code in the sample project did help me.

    In the new system you need to define all assetbundles at once, as a collection of AssetBundleBuilds. In that collection we have one AssetBundleBuild containing our shaders, and in most other bundles we have prefabs that use these shaders. This causes the bundles with the prefabs to not contain the shaders. But it is important that you do *not* mark the shaders as assets in those prefab bundles.

    So in your example with the blob shadow texture, you need to have a bundle with that texture and other bundles that reference the texture but do not explicitly add the blob in the AssetBundleBuild.
    And then it's important to load the assetbundle with the texture first and keep it in memory.
     
    karl_jones likes this.
  41. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    @alexvda you are a legend! This seems to be exactly it. Will experiment with this when I get home.

    I suppose now it is just a simple matter of updating the documention!

    @alexvda I assume youve solved your pink shader issue using this method then? Build asset bundles for all platforms, and when running in editor load the windows/osx version of the asset bundle regardless of your platform settings (Android/iPhone/etc) so that the shaders supported by your editor (which is running on, say, Windows) are loaded as apposed to the asset bundle supported by your editors target platform.
     
  42. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    3,568
    Thanks @alexvda. I'll get the docs team to update the managing dependencies page.
     
    Prodigga likes this.
  43. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    219
    It solved the pink shaders issue indeed. In one case it wasn't solved: when the prefab also used UI elements, then the shaders were still pink. I'm currently still applying the hack for those, so it might be solved with the latest version of Unity, but I'll have to test that.
     
  44. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    219
    I removed the hack, it seems the pink shader issue is completely gone for me.
     
    Prodigga and holliebuckets like this.
  45. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    What Unity version are you using? Also, should this bug be happening even if all of your shaders, materials or textures referenced in the material are in resources folder, and not an asset bundle?
     
  46. diekeure

    diekeure

    Joined:
    Jan 25, 2013
    Posts:
    219
    I'm using the latest 5.2.3f1, not sure what you mean with your second question, but I have nothing in a Resources folder.
     
    SimteractiveDev likes this.
  47. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    790
    Benzino, it would still happen because the asset bundles do not exclude assets inside of the Resources folder. (If that's what you are asking)
     
    SimteractiveDev likes this.
  48. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Ah ok, I see. Thanks!
     
  49. SkaarjSlayer

    SkaarjSlayer

    Joined:
    Oct 10, 2013
    Posts:
    108
    I get this issue as well and it's really troubling. For a while I thought I was doing Asset Bundles wrong until @Sparrowfc post about it having to do with shaders compiled for a platform the editor doesn't support and now it's starting to make a little more sense as to what's going on.

    Are we certain that this is a bug that only affects the Editor? Are we sure that this isn't affecting actual builds that use asset bundles as well?
     
  50. Sparrowfc

    Sparrowfc

    Joined:
    Jan 31, 2013
    Posts:
    100
    pretty sure