Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
[RequireComponent (typeof(MeshRenderer))]
[RequireComponent (typeof(MeshFilter))]
public class DynamicCreateMesh : MonoBehaviour
{
[SerializeField] Material _colorMaterial;
private void Start ()
{
var mesh = new Mesh ();
mesh.vertices = new Vector3[] {
new Vector3 (0, 1f),
new Vector3 (1f, -1f),
new Vector3 (-1f, -1f),
};
mesh.triangles = new int[] {
0, 1, 2
};
float texSize = 256f;
mesh.uv = new Vector2[] {
new Vector2 (31f / texSize, 234f / texSize),
new Vector2 (241f / texSize, 66f / texSize),
new Vector2 (64f / texSize, 27f / texSize),
};
mesh.RecalculateNormals ();
var filter = GetComponent<MeshFilter> ();
filter.sharedMesh = mesh;
var meshRenderer = GetComponent<MeshRenderer>();
meshRenderer.material = _colorMaterial;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment