- Home /
Procedural array of meshes problem / solved
Hi guys! Here is my attempt to generate and render an array(list) of simple rectangle meshes. List contains 100 items but Unity renders the only one (i suppose that the problem is because of incorrect using of MeshFilter but im not sure). Also i tried to make simple color changing with OnMouseOver() and here is also fail. Any suggestions? Thanks in advance for help!
*on a screenshot the cube is only measure tool.
the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class test_cuba : MonoBehaviour {
List <Mesh> shtuchki = new List<Mesh>();
void Start () {
for(int n= 0; n < 10; n++){
for (int m=0; m<10; m++){
MeshFilter mfilter = gameObject.GetComponent<MeshFilter>();
Mesh messh = new Mesh();
mfilter.sharedMesh = messh;
Rect rt = new Rect (n,m,1,1);
/*Debug.Log ( rt.x);
Debug.Log (rt.y);*/
messh.vertices = new Vector3 [] {
new Vector3(rt.x,rt.y,0), // ver0
new Vector3(rt.xMax,rt.y,0), // ver1
new Vector3(rt.xMax,rt.yMax,0), // ver2
new Vector3(rt.x,rt.yMax,0), //ver3
};
messh.triangles = new int [] {
0,1,2,
2,3,0,
};
renderer.material.color = Color.white;
messh.RecalculateBounds();
messh.RecalculateNormals();
shtuchki.Add(messh);
Debug.Log("mesh_number: " + shtuchki.Count);
}
}
}
void Update () {
for(int v=0; v<shtuchki.Count; v++){
Graphics.DrawMeshNow(shtuchki[v], Vector3.zero, Quaternion.identity, 0);
}
}
void OnMouseOver() {
renderer.material.color = Color.red;
}
}
ZIP project: https://www.dropbox.com/s/bj883s3ovnrvpuu/graphs.zip
$$anonymous$$aybe the solution is to create for each mesh element in array its own GameObject with $$anonymous$$eshFilter an $$anonymous$$esh Collider and then unite them as children of a main parent GameObject?
Answer by FakeBerenger · Nov 07, 2012 at 11:14 PM
You're writing over the same mesh at each loop iteration, so the only mesh you see is the last one. You can either append all the triangles in a single mesh (but you won't be able to change the color of a single one, not easily at least), or as Kerbo says, create a game object with it's own meshFilter for each triangle.
Don't forget to add a mesh collider, or the OnMouseXXX won't work (they use raycasts)
Your answer
Follow this Question
Related Questions
Trouble Creating a Procedural Mesh on a Rotated Game Object 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Converting a number of runtime-created prefab GameObject cube's for export 0 Answers
Generate a highpoly sphere with mesh collider procedurally 2 Answers
How do you completely clear a mesh? 1 Answer