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".
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<MeshRenderer> ();
if (unlitMaterial != null)
meshRenderer.material = unlitMaterial;
meshRenderer.material.mainTexture = texture;
gameObject.AddComponent<MeshFilter> ().mesh = mesh;
}
}
(I tried different materials such as unlit/texture with no effect)
Thanks a lot for any help ;-)