[0.6.7] Material/Shader issues with loaded assets in editor

I just updated to 0.6.7 and 2018.3.8f1 and had to completely redo all of my Addressables settings, afterwards all the assets that are loaded via the Addressables.Load call are showing up pink in editor. Doing a Shader.Find() call immediately after the asset is instantiated fixes the problem, but I don’t believe this is something that should be necessary.

The pink texture error can also be “fixed” in playmode by selecting the material, and just re-selecting the shader that it is currently using. Of course this only works during the current playmode session.

Curiously enough, the same issue that is appearing in editor does NOT happen on device. (Testing on a Pixel 2) All assets loaded via the addressables on device do not load with the material/shader error, and don’t require the Shader.Find() fix.

I have not tested on iOS yet.

I made a new project with nothing but a cube as an addressable asset, and a simple script to load in editor as follows.

using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class SimpleAssetLoader : MonoBehaviour
{
    public bool useAddressable = false;
    public bool applyPostLoadShaderFix = false;

    public string assetAddress;
    public GameObject assetReference;

    private GameObject _assetInstance;

    private void Start()
    {
        if (useAddressable)
        {
            Addressables.LoadAsset<GameObject>(assetAddress).Completed += OnLoadAssetCompleted;
        }
        else
        {
            _assetInstance = Instantiate(assetReference) as GameObject;
            Debug.Log($"GameObject {_assetInstance.name} instantiated via asset reference");
        }
    }

    private void OnLoadAssetCompleted(IAsyncOperation<GameObject> loadOp)
    {
        if (loadOp.Result != null)
        {
            _assetInstance = Instantiate(loadOp.Result) as GameObject; // Asset instantiated with silent shader error causing pink material.
            Debug.Log($"GameObject {_assetInstance.name} instantiated via Addressables");

            // This will fix the shader issue. This should not be required.
            if (applyPostLoadShaderFix)
            {
                _assetInstance.GetComponent<Renderer>().sharedMaterial.shader = Shader.Find("Standard");
            }
        }
        else
        {
            Debug.LogError("AsyncOperation failed to load GameObject");
        }
    }
}

Here is the test project I was using in case that is helpful to anyone.

4308658–387379–AddressablesTest.unitypackage (88.7 KB)

What play mode are you running in? If doing Packed Mode, are you sure you ran “Build Player Content” ahead of going into play mode?

-Bill

Yes, running in packed play mode. Build player content was done before playing.

This should be extremely easy to reproduce with the unity package that was provided. If not you can create a new project, make a primitive cube prefab, and throw the script above onto a game object in the scene. The addressables settings were not changed from the default. Maybe I’m missing something, but if the asset is loaded using an Addressables.LoadAsset() call I’m assuming that the settings are all correct. The issue with the shader not being correctly applied would appear to be an issue with how the addressables is loading that asset. And as I said this is working on device, but in editor, it’s pink so I don’t think there is an error in the setup, just the execution.

Created a new project and imported my own unity package, had to install the addressables package after import. The cube will render fine on the standalone platform. Switching build platforms to Android causes the shader issue. I’m taking a stab in the dark here but there would appear to be an assumption made somewhere that incorrectly assumes the platform is the standalone player if we’re in editor.

Here is the project that the package was exported from.

Here is a new project that I made and imported that package to. After import I had to reinstall the addressables through the package manager. On the PC/Linux Standalone platform the cube renders without issue.


After switching to Android, and rebuilding player content, running in packed playmode, the result is the same.

Switching platform to iOS and rebuilding player content, running in packed playmode, results are the same as Android.

From the project the package was imported into.
Doing a Build & Run on a Pixel 2 results in the cube not rendering at all.
Building and side loading an apk onto the same device results in the cube not rendering at all.

From the project the package was exported from.
Doing a Build & Run on a Pixel 2 results in the cube rendering without issue.
Building and side loading an apk onto the same device results in the cube rendering without issue.

I am extremely confused by this last bit.

I have had a colleague at another studio reproduce these results with the package provided above.

UPDATE
It appears the reason that there were issues on device is because after switching from iOS back to Android I simply did a Build Player Content. I tried doing a Clean->All and then another Build Player Content and the cube will render on device. Still pink in the editor though.

Seems like the same issue I have with TextMeshPro but then with WebGL

With the latest package I see more shaders failing in the editor. My custom shader is failing to load as well. In build it is just fine.

For now adding the following workaround to get the shaders to appear correctly in the editor seems to work. Putting this in your Completed handler should suffice until there is an actual fix. In the above script this would replace the if(applyPostLoadShaderFix) block.

#if UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
            // If this seems ridiculous, that's because it is.
            Material sharedMat = _assetInstance.GetComponent<Renderer>().sharedMaterial;
            sharedMat.shader = Shader.Find(sharedMat.shader.name);
#endif

It does not “incorrectly assume” the platform is standalone. The correctly-knows the platform is standalone. You are running the editor in windows/mac. Not on an android device. If you build asset bundles for ios or android, the contents of those bundles are built to be run only on those platforms. There are some particular data types, and some particular scenarios where the android/ios data happens to be the same, so loading it works. But in general, trying to use android or ios bundles in the editor will not work. Whether you are using addressables or not.

To run in the editor, in packed mode, you need to have built those bundles for stand alone. I will make a note to add this to the docs.

2 Likes

Well if that is the case that we always have to build addressables for Standalone for the editor… is there an easier way to do this than to switch to the Standalone platform → build bundles → switch back → test again?
Like a button for Build Bundles for Editor and then make a profile Editor which takes the Editor bundles to load no matter what the current active platform is?

We’re working on a more convenient setup, but it will only be a partial fix. We cannot build content for the non-current platform. That’s just something baked into Unity right now (which another team is working to fix).

The main piece we are working to fix in the near term is to make sure all files we generate are in platform specific folders. As it is now, a few are not, which means building for standalone, then building for android, nukes a couple important files (blocking you from running stand alone without re-building).

also, running in fast mode or virtual mode should work in the editor regardless of the currently set “build target”. If that doesn’t work, please let me know.

-Bill

1 Like

Sure but it will not simulate as if you loaded from
Bundles right? The problem I had with scriptable objects being an instance from the bundle and not the built in ones was one of the issues you wouldnt get with the other modes. Which is why I had to fix quite a few references as I only found out that the problem existed because I went to packed mode or made a build

1 Like

It’s known issue with materials from bundles targeted not to the same device as editor.

To fix it in editor on Win - you can just set Graphics API as OpenGLES3 (Disable “Project Settings → Player → PC, Mac & Linux Standalone Settings → Other Settings → Rendering → Auto Graphics API for Windows” and add “OpenGLES3” as first one).
It’s funny that standalone settings affect the editor, but it helps…))

10 Likes

@unity_bill I also faced the same problem when running packed mode and in build for webgl, in my sample project all i did was that i have a AssetReference, and assetRef.Instantiate(). I also mark a cube as addressable so it endup in DefaultLocalGroup as well as unchecking the Strip engine code under player setting In packed mode, after i build player content, it shows me a purple cube, and when i built into webgl, it won’t spawn the cube anymore and shows me error, i uploaded the screenshot. Please help, I almost cried after testing and debugging this thing. Please help.

1 Like

I had the same problem. Materials on prefabs don’t get loaded properly via adressables.
Setting the Win Graphics API to the same as mobile fixed the issue.

2 Likes

This worked for me.

2 Likes

Doesn’t work. While on WebGL platform all of ‘Use Asset Database’, ‘Simulate Groups’ or ‘Use Existing Build’ options still give magenta everything on instantiating prefabs. Win10, Unity 2020.1.8.

edit - the ‘add GLES3’ fix above works.

1 Like

Thanks you very much!

1 Like

After the almost 3 years later, I have the same issue. It is really unbelievable. I’m using Unity 2020.3.14(LTS) and Addressables 1.16.19(verified). I have lost 3 days of work power with the tests and reading documentation to understand what is wrong with it. Pufff…:rage:

Thanks for the help.

Thanks for help!