Advertisement

How do I load a custom material?

Started by May 13, 2015 01:37 PM
-1 comments, last by SelethD 9 years, 6 months ago

I am sure this is something that has been answered a million times over, but for the life of me, I cant seem to figure it out, nor can I find any helpful info.

I have a mesh (quad) and meshrenderer created in c#. I would like to give the meshrenderer a material that already exists in my resources folder.... Assets/Resources/JmaMaterial

I load the material with... Material mat = Resources.Load<Material>("JmaMaterial");

If I assign it to my meshrenderer like this....

Material BodyMat = new Material(mat);
mMeshRenderer.material = BodyMat;
This works, no problem... however, if I want to add 2 materials to my quad (one for character body, another for chatacter hair)
It doesnt seem to work, this is how im doing it...
mMeshRenderer.materials = new Material[2];
Material mat = Resources.Load<Material>("JmaMaterial");
if (mat != null)
{
mBodyMat = new Material(mat);
mHairMat = new Material(mat);
mMeshRenderer.material[0] = mBodyMat;
mMeshRenderer.materials[1] = mHairMat;
}
else Debug.Log("failed to find material");

So... obviously im doing something wrong, I try to Google about assigning multiple materials to a mesh, but all the results either have no solution, or they explain how to do it in the editor, not in code.

Is this even the correct way to do an image with multiple parts? (body + hair) Instead of having one quad with 2 materials, should I instead go with 2 mesh quads and each have 1 material?

Thanks for any help

This topic is closed to new replies.

Advertisement