• Unity
  • How to use Multiple Materials?

  • Düzenlendi
Related Discussions
...

Hello!

I'm working on a project that uses a custom shader for the rendering of the characters. This shader has a lot of different features and it's working fine. Due to some technical reasons, we can't add another vertex pass to the shader, so we planned on adding an additional material on our mesh renderer to achieve a certain effect.

The problem is that for some reason we can't add another material to the mesh renderer, we tried doing it through the editor and runtime, both attempts with no success.

How could we add 2 materials to our spine model that get rendered simultaneously?

Thanks in advance.

Hello!

I have tried implementing the solution as follows:

public void Awake()
{
    skel.OnMeshAndMaterialsUpdated += UpdateMats;

}

private void UpdateMats(SkeletonRenderer skeletonRenderer)
{
    meshRenderer.materials.Add(meterialIWantToUse);
}

The code did run as I expected. However, the second material just replaces the previous one, the same as if I was using the included material override tools.

The behavior we want to reproduce is to have both run at the same time, as if the mesh renderer had two materials running simultaneously, instead of swapping them around.

We want to be able to achieve this:

Either through code or editor.

EDIT: Hello again! We managed to solve things around. I forgot you can't directly set the materials. The following code works as expected:

public void Awake()
{
    skel.OnMeshAndMaterialsUpdated += UpdateMats;

UnityEngine.Material[] sharedMaterial = meshRenderer.sharedMaterials;
 vMats = new UnityEngine.Material[sharedMaterial.Count() + 1];
    vMats[0] = materialToReplace;
    vMats[1] = materialIWantToUse;


}

private void UpdateMats(SkeletonRenderer skeletonRenderer)
{
    meshRenderer.materials = vMats;
}

Glad you've figured it out, thanks for letting us know!

Your first posting of adding a material would not have made a lot of sense, as you then did you need to either replace an existing one or make sure that the number of materials matches the number of submeshes.