How to change _MainTex and _DetailAlbedoMap unity webgl Addressables

First you need to enable _DetailAlbedoMap

material.EnableKeyword(“_DETAIL_MULX2”);

than you need to change _DetailAlbedoMap and after that change _MainTex. It works fine for me.

Full code here:

private Material ChangeMaterial(Material material)
    {       
        Material newmat = material;
        newmat.EnableKeyword("_DETAIL_MULX2");
        newmat.SetTexture("_DetailAlbedoMap", material.mainTexture);  
        newmat.SetTexture("_MainTex", seamTextureAlbedo1);
        newmat.SetColor("_BaseColor", new Color(188, 188, 188, 255));
        newmat.SetFloat("_Cutoff", 0.05f);
        newmat.SetFloat("_Glossiness", .0f);    

        return material;
    }

But this code works fine in unity editor and doesn’t work in webgl site – changing only _DetailAlbedoMap or _MainTex. i spenŠµ a several days to find the way and the way is create another material and put it in Addressables and after that loaded it in script, changing it where if you need.

Of course this is good for not so many materials. if you have many materials to change, this is not good method, more better to change material by code.

private void Start()
    {
        LoadAllSeamColors();
    }

private void LoadAllSeamColors()
    {
        StartCoroutine(LoadAllSeamMaterials("doris1"));
        StartCoroutine(LoadAllSeamMaterials("doris2"));
        StartCoroutine(LoadAllSeamMaterials("doris3"));
        StartCoroutine(LoadAllSeamMaterials("doris4"));
        StartCoroutine(LoadAllSeamMaterials("doris5"));
    }

private IEnumerator LoadAllSeamMaterials(string path)
    {
        var _loading0 = Addressables.LoadAssetAsync<Material>(path);
        yield return _loading0;
        if (_loading0.Status == AsyncOperationStatus.Succeeded)
        {
            seamMaterials.Add(_loading0.Result);
        }
    } 

public void ApplyNewThreads(Material material)
    {      
       thread.material = ChangeSeamMaterial(material);
    }

//changing material
private Material ChangeSeamMaterial(Material material)
    {//find materials by name of _DetailAlbedoMap 
        return seamMaterials.FirstOrDefault(x =>       x.GetTexture("_DetailAlbedoMap").name == material.mainTexture.name);
    }

Live a comment if you like!

Leave a Reply

Your email address will not be published. Required fields are marked *