Instantly share code, notes, and snippets.

@esakun /DynamicCreateMesh.cs Secret
Last active Jun 25, 2016

Embed
What would you like to do?
Unityで動的にメッシュを作ってみる。
using UnityEngine;
using System.Collections;
namespace Esakun.Sample
{
/// <summary>
/// 頂点カラーで三角形を描画します
/// </summary>
[RequireComponent (typeof(MeshRenderer))]
[RequireComponent (typeof(MeshFilter))]
public class DynamicCreateMesh : MonoBehaviour
{
[SerializeField]
private Material _mat;
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
};
// 変更箇所 : 各頂点に色情報を設定
mesh.colors = new Color[] {
Color.white,
Color.red,
Color.green
};
var filter = GetComponent<MeshFilter> ();
filter.sharedMesh = mesh;
var renderer = GetComponent<MeshRenderer> ();
renderer.material = _mat;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment