Search Unity

Issue with shaders in the editor when targeting android

Discussion in 'Asset Bundles' started by donnysobonny, Jan 6, 2021.

  1. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    Tested in 2019.3.9f1, 2019.4.17f1 and 2020.2.0f1

    The general issue is that when we are using android asset bundles in the editor (so pulling assets from an asset bundle that was built with android as the build target), shaders don't seem to work as expected. Some shaders seem to resort to their fallbacks, while other shaders don't seem to render at all (displaying in pink). The same assets/shaders work fine when included in the scene (not from an asset bundle).

    Some pre-reproduction notes:

    - this issue seems to only happen in the editor (tested building to both android and ios devices, and the issue does not exist when tested on a device)
    - tested in the editor both on a mac and on windows, same issue on both
    - seems to work fine when targeting ios as the build target. Only seems to be an issue when targeting android as the build target

    To reproduce:

    - ensure you are targeting android as the build target
    - set up some rendered objects with shaders to be bundled up. It seems to be that opengl shaders may be the issue. A shader that has no directx fallback seems to result in failure to display (will render pink) where as a shader with a directx fallback will usually fall back to that (mostly results in falling back to the "Diffuse" shader)
    - build your bundles for android (note that we're not using addressables here, so just using the BuildPipeline)
    - set up a test environment that will download the asset bundle and then load and instantiate the objects into the game world and you will see that the shaders have either not rendered or have fallen back

    Some current fixes/workarounds:

    - if you force the graphics api to be the same graphics api that android will be using, this solves the problem. So, if you're on a windows machine and you go to the player settings, you will notice that (by default) android defaults to the vulkan graphics api. If you disable "auto graphics api" for windows, and set windows to use vulkan as the default graphics api, the problem is solved
    - if, instead of using android asset bundles, you use asset bundles that are built for your machine (so if you work on a windows machine, build windows asset bundles. On a mac, build osx bundles) this seems to solve the problem

    I am aware that it could be intentional that you use asset bundles that target your current system, rather than your current build target, but this seems like unexpected behaviour/counter-intuitive so I just wanted to double check
     
    Last edited: Jan 6, 2021
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Isn't this by design?
    The asset bundle must be built for platform is is loaded on.
     
  3. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    I have considered that that might be the case, but I am under the impression that if I have set android as the build target, unity should be assuming that it's platform is android. I say this because, consistently, this seems to be how unity works normally. For example, Application.platform and EditorUserBuildSettings.activeBuildTarget will both point to android, and UNITY_ANDROID will also be true... so, as far as unity seems to be concerned, it is on the android platform, just not on the android operating system...

    I would be forced to use something like SystemInfo.operatingSystemFamily in order to determine how I should build the bundles to be tested in the editor, which just seems strange/inconsistent/counter-intuitive. It also means that if I am doing (what I assume is) a common thing such as putting asset bundles inside of StreamingAssets in order to test bundles locally, I have to have separate bundles for the ones that I test in the editor, versus the ones that I have to test on the device, essentially meaning that I cannot test the actual bundles that the end-user will use without building to a device...

    All-in-all, if what you say is intended design, it seems like inconsistent/counter-intuitive design...

    I fear though that, if this is not a bug, it probably couldn't be considered a feature request, considering that this could cause backwards incapability issues?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    No, that's not how it works.
    All of the above let you run the script code as if you were on Android platform, however, there is also UNITY_EDITOR define, which lets you find out whether you are on Editor or an actual device.

    Asset bundles contain non-code related data and it is platform specific. See:
    https://docs.unity3d.com/ScriptReference/AssetBundle.html
     
  5. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    Got it. Thanks for the clarification!
     
  6. mcarriere

    mcarriere

    Joined:
    Sep 14, 2012
    Posts:
    106
    This is the second time we've run into this issue on the same project. Unfortunately the first time happened during a Unity update a year or so ago. There was a small frenzy looking into why our AssetBundles were not working on the Android platform, we found this forum post and breathed a sigh of relief and moved on. Only to forget those details ~18 months later and repeat the same pattern. I'd like to get a more direct reply on record here for ourselves and others if possible.

    The title we're working on is only a mobile release; specifically iOS and Android. More of our devs are using macOS over Windows.

    Is the solution being suggested here that -- if we want to continue the use of Asset Bundles -- we maintain versions of Asset Bundles that will only be used in the Editor?