1

In Unity URP when i load Assetbundle from server all it's materials shaders are correct but object becomes PINK. Any Idea how to resolve this?

Abhishek Jain
  • 179
  • 2
  • 7
  • Pink materials means broken/missing shaders .. my guess your shaders are not compatible with URP – derHugo Jan 17 '22 at 13:13
  • There is possibility that you build assetbundle for different platform than you loading platform. like maybe you build assetbundle for android and then you loading it into unity editor or other platform. in this case this issue occurs. – Jaimin Jan 18 '22 at 04:07
  • i build AssetBundle for Android. and when i retreive Assetbundle they already have correct material property. I just have to reslect it again. – Abhishek Jain Jan 18 '22 at 05:47

1 Answers1

1

The materials might appear pink only in the Editor. It should work fine in build.

However, it can get pretty annoying to see pink color everywhere on the game screen. You can use the below script to fix this in Editor.

using TMPro;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_EDITOR
    public static class AssetBundleEditorUtil
    {
        public static void FixShadersForEditor(GameObject prefab)
        {
            var renderers = prefab.GetComponentsInChildren<Renderer>(true);
            foreach (var renderer in renderers)
            {
                ReplaceShaderForEditor(renderer.sharedMaterials);
            }

            var tmps = prefab.GetComponentsInChildren<TextMeshProUGUI>(true);
            foreach (var tmp in tmps)
            {
                ReplaceShaderForEditor(tmp.material);
                ReplaceShaderForEditor(tmp.materialForRendering);
            }
            
            var spritesRenderers = prefab.GetComponentsInChildren<SpriteRenderer>(true);
            foreach (var spriteRenderer in spritesRenderers)
            {
                ReplaceShaderForEditor(spriteRenderer.sharedMaterials);
            }

            var images = prefab.GetComponentsInChildren<Image>(true);
            foreach (var image in images)
            {
                ReplaceShaderForEditor(image.material);
            }
            
            var particleSystemRenderers = prefab.GetComponentsInChildren<ParticleSystemRenderer>(true);
            foreach (var particleSystemRenderer in particleSystemRenderers)
            {
                ReplaceShaderForEditor(particleSystemRenderer.sharedMaterials);
            }

            var particles = prefab.GetComponentsInChildren<ParticleSystem>(true);
            foreach (var particle in particles)
            {
                var renderer = particle.GetComponent<Renderer>();
                if (renderer != null) ReplaceShaderForEditor(renderer.sharedMaterials);
            }
        }

        public static void ReplaceShaderForEditor(Material[] materials)
        {
            for (int i = 0; i < materials.Length; i++)
            {
                ReplaceShaderForEditor(materials[i]);
            }
        }

        public static void ReplaceShaderForEditor(Material material)
        {
            if (material == null) return;

            var shaderName = material.shader.name;
            var shader = Shader.Find(shaderName);

            if (shader != null) material.shader = shader;
        }
    }
#endif