Search Unity

Missing Transparent/Diffuse shader when building

Discussion in 'Editor & General Support' started by ima747, Sep 24, 2013.

  1. ima747

    ima747

    Joined:
    Aug 14, 2013
    Posts:
    5
    I am setting a renderer material shader to "Transparent/Diffuse" in my code. This has worked until the recent update 4.2.1f4 (don't recall what I was using previously but I updated a couple of times without issue). It still works fine in Unity but when I build a target (Specifically Mac OS, x86, or 64 bit, or universal), I get pink meshes since they appear to have no renderer. I assume I need to manually add the renderer to the project but I don't know where to find it. I also can't locate it in the unity editor when trying to set it for a mesh renderer (extra strange since loading it in code works in the editor, but not in the build).

    I have no idea where to go to resolve this... I suspect it has something to do with Unity updating my project with the latest update (which also means I can't go back to an older version...)
     
  2. reddotgames

    reddotgames

    Joined:
    Apr 5, 2011
    Posts:
    705
    When you changing material from code - make sure you have those shaders in Resources folder. But only custom ones - buildin shaders should always work - even when changing from code.
    Maybe made your copy of that shader - call if different, put in Resources folder and try like this.
     
  3. ima747

    ima747

    Joined:
    Aug 14, 2013
    Posts:
    5
    The issue with copying the shader is that I don't know where it comes from. It doesn't show up in the Unity editor any more, the only way it loads is from code executed in the editor. I can't set it through the IDE, and if I build it doesn't load from code any more either.
     
  4. ima747

    ima747

    Joined:
    Aug 14, 2013
    Posts:
    5
    With more investigation it appears to be a problem specifically with OS X builds. Building for web works fine. I put in some logging and when building for OS X targets Shader.Find("Transparent/Diffuse"); returns null. In the editor (running on OS X for reference) it works fine, and web plugin targets work fine. I suspect this is a bug with the latest Unity build that doesn't properly copy the base shaders for OS X targets unless they are attached to an object already. Maybe an optimization pass that's too aggressive or doesn't take into account that things might just be code based?

    My solution is to create a cube, assign a texture to it, then set the shader to Transparent/Diffuse, then disable the object in the editor. This is ugly and hacky but it forces the shader to compile in.
     
  5. Kaspar-Daugaard

    Kaspar-Daugaard

    Unity Technologies

    Joined:
    Jan 3, 2011
    Posts:
    150
    We did get more aggressive about stripping shaders from builds if it looks like they are unused. For a cleaner workaround check out Project Settings -> Graphics -> Always Included Shaders.
     
    PazChen and DDeathlonger like this.
  6. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    If you need the build in shaders , you can download them for any unity version here:
    http://unity3d.com/unity/download/archive

    So you can copy the shader into your project and assign them to a variable of your script, so you do not need to make Shader.Find().

    Not sure, but maybe unity is stripping the unused shaders from the project/built in shaders.

    Edit: thank you Kaspar Daugaard for your better explanation.
     
  7. ima747

    ima747

    Joined:
    Aug 14, 2013
    Posts:
    5
    Perfect Kaspar, great to know that a) I'm not going crazy b) my hunch was right and c) there's a proper fix implemented in the system.

    Of note, I was still getting the shader in web exports so presumably those aren't scrubbing as deeply (I like optimization so now that I know how to work around this I'm a happy camper and would like to see more cleaning on more targets).
     
  8. Kaspar-Daugaard

    Kaspar-Daugaard

    Unity Technologies

    Joined:
    Jan 3, 2011
    Posts:
    150
    The web plugin still ships with those shaders installed to handle backwards compatibility. Since it's part of the plugin it doesn't add to the size of the game. It's of course a bit confusing that web content works differently.