Unity/C#: procedural texture for procedural mesh
<p>I would like to generate meshes and textures procedural in Unity/C#. However all my attempts with a custom mesh only display the first texel. I tried to simplify the problem as much as possible and came up with the following example code, which draws a white square instead of a "polish flag".</p>

<pre><code>using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    [SerializeField] Material unlitMaterial;

    void Start () {
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[4] { Vector3.zero, new Vector3 (1, 0, 0), new Vector3 (1, 0, 1), new Vector3 (0, 0, 1) };
        mesh.triangles = new int[6] {1, 0, 3, 2, 1, 3 } ; 
        mesh.RecalculateNormals();

        Texture2D texture = new Texture2D (2, 2);
        texture.SetPixels (new Color[4] {Color.white, Color.white, Color.red, Color.red});
        texture.filterMode = FilterMode.Point;
        texture.Apply();

        GameObject gameObject = new GameObject ();
        MeshRenderer meshRenderer = gameObject.AddComponent&lt;MeshRenderer&gt; ();
        if (unlitMaterial != null)
            meshRenderer.material = unlitMaterial;
        meshRenderer.material.mainTexture = texture;
        gameObject.AddComponent&lt;MeshFilter&gt; ().mesh = mesh;
    }
}
</code></pre>

<p>(I tried different materials such as unlit/texture with no effect)</p>

<p>Thanks a lot for any help ;-)</p>