🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do I load a custom material?

Started by
-1 comments, last by SelethD 9 years, 1 month 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