「Unity/Csharp/mesh」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→単色三角形サンプル) |
|||
行6: | 行6: | ||
PlayScene.cs | PlayScene.cs | ||
<pre> | <pre> | ||
− | + | GameObject rObject = GameObject.Find("RObject"); | |
− | + | 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.RecalculateNormals(); | |
− | + | MeshFilter filter = rObject.GetComponent<MeshFilter>(); | |
− | + | filter.sharedMesh = mesh; | |
− | + | MeshRenderer renderer = rObject.GetComponent<MeshRenderer>(); | |
− | + | renderer.material = rObject.GetComponent<PlaySceneRObject>().redMaterial; | |
− | + | filter.sharedMesh = mesh; | |
</pre> | </pre> | ||
2020年9月15日 (火) 11:16時点における最新版
単色三角形サンプル
- 新規でGameObjectを作成しCanvasに追加(ここではRObject)
- ヒエラルキーに新規でMaterial(ここではBlueMaterial)を作りUnlit/Colorを選択し色を選択。
- RObjectにPlaySceneRObject.csをつぶける
- Robjectを選択し、プロパティにあるblueMaterialにBlueMaterialをドラッグ
PlayScene.cs
GameObject rObject = GameObject.Find("RObject"); 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.RecalculateNormals(); MeshFilter filter = rObject.GetComponent<MeshFilter>(); filter.sharedMesh = mesh; MeshRenderer renderer = rObject.GetComponent<MeshRenderer>(); renderer.material = rObject.GetComponent<PlaySceneRObject>().redMaterial; filter.sharedMesh = mesh;
PlaySceneRObject.cs
public class PlaySceneRObject : MonoBehaviour { public Material blackMaterial; public Material blueMaterial; public Material greenMaterial; public Material orangeMaterial; public Material pinkMaterial; public Material purpleMaterial; public Material redMaterial; public Material whiteMaterial; public Material yellowMaterial; }
参考
http://ftvoid.com/blog/post/800
https://www.shibuya24.info/entry/2015/11/29/180748
座標
xとyが正の場合は右上に向かって伸びる
mesh.vertices = new Vector3[] { new Vector3 (0f, 0f), new Vector3 (1f, 1.1f), new Vector3 (1f, 1f), };