meshrenderer how to add texture code example

Example: how to assign a texture to a Meshrenderer unity

// Initialize a 2D texture of sizeX by sizeY
Texture2D texture = new Texture2D(sizeX, sizeY);

// Set the colors to it (an array of colors), 
// but you can also use texture.SetPixel(x, y, color) to set
// the pixel at (x, y) coordinates to the color 'color'
texture.SetPixels(colors);
// DON'T FORGET TO APPLY THE TEXTURE (VERY IMPORTANT)
texture.Apply();

// Set it to your MeshRender
meshRenderer.sharedMaterial.mainTexture = texture;