This is something that has been really bugging me, and I thought he would be a good place to ask.
So I have a custom mesh, and I would like to have it in a for loop. basically taking it so it would make the mesh, then make another mesh without removing the previous one. I don’t feel like copying the code, but here is my best pseudo-code (Not actually copy-paste code!!):
Vector3[] verts;
int[] tris;
Mesh mesh;
// normal custom mesh code here
for (int i = 0; i < 50; i++) {
verts = new Vector3[
new Vector3(-i, 0, -i),
new Vector3(i, 0, -i),
new Vector3(i, 0, i)
];
tris = new int[
0, 1, 2
];
mesh.vertices = verts;
mesh.triangles = tris;
}
is there anyway to do something like instantiate, or is it not possible?
Thanks, but that doesn’t really help as this isn’t procedural generation. Im wondering if I can make several custom meshes with only one empty gameobject with a mesh filter and mesh renderer. All done with a for loop.
You can make an array of meshes and sequentially assign them to the MeshFilter on your GameObject, and if it has a MeshRenderer, then it will show that newly assigned mesh.
To do it:
During initialization:
make an array of meshes
iterate and create each mesh and assign it into the slot
During running:
choose what the next mesh should be
assign that mesh to the MeshFilter of the GameObject
Regardless of where you get your source data, you’d still create the mesh in the exact same way. If you’re pulling all the data that you need from a file, it should be possible to adapt that example to suit your needs.
I already have code to make the mesh. I wanted to use a for loop to make every mesh in the file. @Kurt-Dekker understands what I’m trying to do. Use a for loop to make x amount of custom meshes by assigning each new one to a new game object.
Did you want them in separate GameObjects or all submeshes of the same GameObject?
Never mind~~,~~ you said seperate objects. So, do these GameObjects already exist or do you want to create them as you go?
Right, that’s why I posted that link about procedural mesh creation a few posts back. It shows how to create a mesh object from triangles and vertices.
Edit: by the way, if we just have an array of triangles and array of vertices, than how do get multiple meshes out of that? How do you know which triangle goes to which mesh? Are you planning to read-in the meshes-one-at-a-time and reuse the same array each time?
Basically, yes. I know how to make a mesh. Each object in the array has verts and tris, and they go together. I have a for loop that takes the number of meshes in the array, and for each one makes the mesh. The problem Im having is that each time i make a new mesh the old one gets overwritten, so I would like each mesh to be its own game object
Ok, so I had already posted some code to create the game object in this post: https://discussions.unity.com/t/774445/12
So All you need to do is basically take the code inside of the loop and put that in your own loop instead. Then assign the MeshFilter.mesh to the mesh that you create in each iteration.