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. Dismiss Notice

Question Addressable material showing pink

Discussion in 'Addressables' started by TCYeh, May 16, 2023.

  1. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Hello,

    I'm currently learning to use Addressables to load assets in my Unity project. Everything seems to be working fine, except for the materials. No matter what material I load, it always displays as pink in the game scene, including the TMP_Font Assets.

    Here's what I have done so far:
    • I have marked all the materials as addressable.
    • I have enabled the shader stripping option.
    However, the issue persists. Can anyone help me identify what I might be missing or doing wrong?

    Additionally, I have included the code I'm using to load the assets:
    Code (CSharp):
    1.  
    2.     void Start()
    3.     {
    4.         Debug.Log("Initializing Addressables...");
    5.         Addressables.InitializeAsync().Completed += AddressableManager_Completed;
    6.     }
    7.  
    8.     void AddressableManager_Completed(AsyncOperationHandle<IResourceLocator> obj)
    9.     {
    10.         Debug.Log("Initializing Addressables...");
    11.         cubeReference.LoadAssetAsync<GameObject>().Completed += (go) =>
    12.         {
    13.             goLoader.button.interactable = false;
    14.  
    15.             goLoader.cube = go.Result;
    16.             goLoader.cube.name = "GeneratedCube";
    17.  
    18.             goLoader.button.interactable = true;
    19.             Debug.Log("Instantiated the cube...");
    20.         };
    21.  
    22.         materialReference.LoadAssetAsync<Material>().Completed += (material) =>
    23.         {
    24.             goLoader.cube.GetComponent<Renderer>().material = material.Result;
    25.  
    26.             Debug.Log("Loaded material...");
    27.         };
    28.    
    29.         Debug.Log("Loaded Assets...");
    30.     }
    31.  
    32.     [Serializable]
    33.     public class AssetReferenceMaterial : AssetReferenceT<Material>
    34.     {
    35.         public AssetReferenceMaterial(string guid) : base(guid) { }
    36.     }
    I have also attached images of my material, addressable groups, and the game scene for reference.
    material.png addressableGroup.png scene.png

    Thank you in advance for your assistance!
     
  2. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Update:
    I've made progress in resolving the issue. By reassigning the shader to the loaded materials, I could fix the problem. Now, the game objects display with the correct materials.

    However, I encountered a new challenge when applying the same solution to TextMeshPro UGUI objects. Despite my attempts to reassign the shader and font asset, I couldn't achieve the desired outcome.

    In the case of modifying the shader, I used the line "text.fontSharedMaterial.shader = shader.Result" and set all dirty.

    Is there something else I should be considering or any alternative approaches I could try? Any insights would be greatly appreciated.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    I don't think pink materials is an issue of addressables, but the project in general, especially if the material shows pink in your assets window. I often see this with shaders/materials that aren't compatible with your render pipeline, or something amiss in the render pipeline in general.
     
  4. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Hello,
    Thank you for your response.

    I've been testing addressables in Unity Editor version 2021.3.8 with the Play Mode Script set as "Use Existing Build(Android)". The shaders I'm using are "Standard" for GameObjects and "TextMeshPro/Mobile/Distance Field" for texts. These are basic shaders, so I don't anticipate any pipeline issues.

    After a day of experimentation, I discovered that when I load shaders using Addressables.LoadAssetAsync<Shader>, they display in pink. However, if I use Shader.Find instead, everything works correctly.

    At this point, I'm starting to wonder if this is a bug or if there's something else going on...

    Here is my latest code:
    Code (CSharp):
    1.  Addressables.LoadAssetAsync<TMP_FontAsset>("LiberationSans SDF").Completed += (font) =>
    2.         {
    3.             Addressables.LoadAssetAsync<Shader>("Assets/TextMesh Pro/Shaders/TMP_SDF-Mobile.shader").Completed += (shader) =>
    4.             {
    5.                 Shader newShader = shader.Result;
    6.                 //Shader newShader = Shader.Find("TextMeshPro/Mobile/Distance Field");
    7.                 TextMeshProUGUI[] texts = FindObjectsOfType<TextMeshProUGUI>();
    8.                 for (int i = 0; i < texts.Length; i++)
    9.                 {
    10.                     TextMeshProUGUI text = texts[i];
    11.  
    12.                     text.font = font.Result;
    13.  
    14.                     Material mat = text.fontSharedMaterial;
    15.                     mat.shader = newShader;
    16.  
    17.                     Debug.Log($"Assign font material...{i}");
    18.                 }
    19.             };
    20.            
    21.             Debug.Log("Loaded font assets...");
    22.         };
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    Well you shouldn't/don't need to load the game object/prefab and the material and the shader. Usually you just load the game object and all it's dependencies come with it.

    Does mean you need to include said materials and shaders in your group, but you don't have to granularly load every single asset via addressables.
     
  6. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Totally agree that it should be done like that. However, if I only loaded the prefab the object will be pink.
    I've already included the prefab and the related shader and material to the group.

    pinkCube.png
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    And is the material also broken if you just drag the asset into a scene normally? Ergo, as if you were placing a prefab.
     
  8. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Do you mean to drag the prefab into the scene? If so, it works normally.
     
  9. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    Update:
    I did some additional testing on an Android device using the same project, and interestingly, all the materials loaded perfectly fine without requiring any manual reassignment of materials or shaders. (That is, only loading the prefabs themself)

    It seems like the issue might be specific to the Unity Editor when the Play Mode Script is set to "Use Existing Build(Android)". It's quite possible that there's a bug in that particular configuration.
     
  10. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    That makes sense. If you change Play Mode Script to asset database, does it proceed to work again?

    If so, worth a bug report.
     
  11. TCYeh

    TCYeh

    Joined:
    Jul 25, 2022
    Posts:
    51
    @spiney199 Yes, it works while using asset database.
    I'll post a bug report. Thank you for helping me out!
     
  12. OneManEscapePlan

    OneManEscapePlan

    Joined:
    Oct 14, 2015
    Posts:
    207
    It's not a bug - if you load from your Android Addressables, you're loading shaders that were compiled for mobile GPUs and aren't compatible with your computer's GPU.
     
    TCYeh and LuGus-Jan like this.
  13. LuGus-Jan

    LuGus-Jan

    Joined:
    Oct 3, 2016
    Posts:
    169
    ^ This.

    When using play mode through existing bundles play mode option, it will load the bundles in editor for that platform, including the shaders built for that target platform. They aren't compatible if the editor and target build platform aren't exactly the same. For example, this existing bundles play mode option will only work on Windows if you've built and are using the bundles made for the Windows target platform. Using Mac, Linux, Android, etc. bundles on Windows will yield the exact same result: pink materials (and perhaps some more funky stuff if stuff has been specifically serialized under the guise of that target platform).
     
    akareactor, TCYeh and spiney199 like this.