Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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

Discussion in 'Addressables' started by clacey_78, Mar 11, 2019.

  1. clacey_78

    clacey_78

    Joined:
    Jun 6, 2014
    Posts:
    5
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AddressableAssets;
    3. using UnityEngine.ResourceManagement.AsyncOperations;
    4.  
    5. public class SimpleAssetLoader : MonoBehaviour
    6. {
    7.     public bool useAddressable = false;
    8.     public bool applyPostLoadShaderFix = false;
    9.  
    10.     public string assetAddress;
    11.     public GameObject assetReference;
    12.  
    13.     private GameObject _assetInstance;
    14.  
    15.     private void Start()
    16.     {
    17.         if (useAddressable)
    18.         {
    19.             Addressables.LoadAsset<GameObject>(assetAddress).Completed += OnLoadAssetCompleted;
    20.         }
    21.         else
    22.         {
    23.             _assetInstance = Instantiate(assetReference) as GameObject;
    24.             Debug.Log($"GameObject {_assetInstance.name} instantiated via asset reference");
    25.         }
    26.     }
    27.  
    28.     private void OnLoadAssetCompleted(IAsyncOperation<GameObject> loadOp)
    29.     {
    30.         if (loadOp.Result != null)
    31.         {
    32.             _assetInstance = Instantiate(loadOp.Result) as GameObject; // Asset instantiated with silent shader error causing pink material.
    33.             Debug.Log($"GameObject {_assetInstance.name} instantiated via Addressables");
    34.  
    35.             // This will fix the shader issue. This should not be required.
    36.             if (applyPostLoadShaderFix)
    37.             {
    38.                 _assetInstance.GetComponent<Renderer>().sharedMaterial.shader = Shader.Find("Standard");
    39.             }
    40.         }
    41.         else
    42.         {
    43.             Debug.LogError("AsyncOperation failed to load GameObject");
    44.         }
    45.     }
    46. }
    47.  
     
  2. clacey_78

    clacey_78

    Joined:
    Jun 6, 2014
    Posts:
    5
    Here is the test project I was using in case that is helpful to anyone.
     

    Attached Files:

  3. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    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
     
  4. clacey_78

    clacey_78

    Joined:
    Jun 6, 2014
    Posts:
    5
    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.
     
    Last edited: Mar 12, 2019
  5. clacey_78

    clacey_78

    Joined:
    Jun 6, 2014
    Posts:
    5
    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.
    ExportedPackage.PNG

    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.
    ImportedPackage.png
    After switching to Android, and rebuilding player content, running in packed playmode, the result is the same.
    ImportedPackage_Android.png

    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.
     
    Last edited: Mar 12, 2019
  6. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,063
  7. clacey_78

    clacey_78

    Joined:
    Jun 6, 2014
    Posts:
    5
    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.

    Code (CSharp):
    1. #if UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
    2.             // If this seems ridiculous, that's because it is.
    3.             Material sharedMat = _assetInstance.GetComponent<Renderer>().sharedMaterial;
    4.             sharedMat.shader = Shader.Find(sharedMat.shader.name);
    5. #endif
     
  8. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    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.
     
    zyonneo and MNNoxMortem like this.
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,063
    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?
     
  10. unity_bill

    unity_bill

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    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
     
    MNNoxMortem likes this.
  11. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,063
    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
     
    clacey_78 likes this.
  12. nik_d

    nik_d

    Joined:
    Apr 27, 2018
    Posts:
    66
    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..))
     
  13. libra34567

    libra34567

    Joined:
    Apr 5, 2014
    Posts:
    62
    @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.
     

    Attached Files:

    Twyker_gp likes this.
  14. drallcom3

    drallcom3

    Joined:
    Feb 12, 2017
    Posts:
    162
    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.
     
    DavidZobrist and Most_Incredible like this.
  15. Most_Incredible

    Most_Incredible

    Joined:
    Jan 25, 2012
    Posts:
    36
    This worked for me.
     
    DavidZobrist and unity_9Winter like this.
  16. Aurigan

    Aurigan

    Joined:
    Jun 30, 2013
    Posts:
    291
    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.
     
    DavidZobrist likes this.
  17. unity_9Winter

    unity_9Winter

    Joined:
    Feb 23, 2021
    Posts:
    1
    Thanks you very much!
     
    DavidZobrist likes this.
  18. yusuf_isik

    yusuf_isik

    Joined:
    Feb 14, 2014
    Posts:
    21
    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....:mad:

    Thanks for the help.
     
  19. zo12n2kqesfm

    zo12n2kqesfm

    Joined:
    Dec 22, 2022
    Posts:
    1
    Thanks for help!