Make custom mesh if for loop

Sorry it should be “in a for loop”

Hi!

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 for any help, givo

I’m not quite sure what you are trying to accomplish, but I suspect it is completely possible.

I have a project called makegeo that demos a bunch of different procedural mesh generation, so maybe something in there might help you?

MakeGeo is presently hosted at these locations:

https://bitbucket.org/kurtdekker/makegeo

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 meshes with any loop you want.

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

Thanks, but do you have any examples of that? It seems kinda complicated

Google for “Unity procedural mesh generation” with no quotes. There are lots of examples out there. Here is one:
https://catlikecoding.com/unity/tutorials/procedural-grid/

It’s not procedural generation. I’m pulling verts and tris from a json but I simplified it to just use the loop iteration variable to make it easier.

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?

Create as I go. They are in an array so it makes it easier

You have an array of Meshes already? Then you’ve already done the difficult part. Putting them in to GameObjects will be easy.

So let’s say your array is called meshArray. You just need to do the following

GameObject myCurrentGameObject;
MeshFilter myCurrentMeshFilter;
for (int i=0;i<meshArray.Length;i++)
{
     myCurrentGameObject = new GameObject("Mesh Object "+i);
     myCurrentMeshFilter = myCurrentGameObject.AddComponent<MeshFilter>();
     myCurrentMeshFilter.mesh = meshArray[i];
}

That’s just the base-line, you’ll probably want to add some sort of Material too, and other components.

Sorry, I have an array with components made up of an array of vertices and an array of triangles

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.

OK, thanks! I’ll try that! I was unsure if it was goanna work

Thanks! It works! I just had to add a mesh renderer and all good! final code:

var stuff = JSON.Parse(filedata.text);

        GameObject myCurrentGameObject;
        MeshFilter myCurrentMeshFilter;
        MeshRenderer myCurrentMeshRenderer;
        for (int e=0; e<stuff["chunkData"].Count; e++)
        {
            vertsl = stuff["chunkData"][e]["verts"].Count;
            verts = new Vector3[vertsl];
       
            for (var i = 0; i < stuff["chunkData"][e]["verts"].Count; i++) {
                verts[i] = new Vector3(stuff["chunkData"][e]["verts"][i]["X"], stuff["chunkData"][e]["verts"][i]["Y"], stuff["chunkData"][e]["verts"][i]["Z"]);
            }
       
            trisl = stuff["chunkData"][e]["tris"].Count;
            tris = new int[trisl];
       
            for (var r = 0; r < stuff["chunkData"][e]["tris"].Count; r++) {
                tris[r] = stuff["chunkData"][e]["tris"][r];
            }

            myCurrentGameObject = new GameObject("Mesh Object " + e);
            myCurrentMeshFilter = myCurrentGameObject.AddComponent<MeshFilter>();
            myCurrentMeshRenderer = myCurrentGameObject.AddComponent<MeshRenderer>();
            myCurrentMeshFilter.mesh.vertices = verts;
            myCurrentMeshFilter.mesh.triangles = tris;
            int scale = stuff["scale"];
            myCurrentGameObject.transform.position = new Vector3(stuff["chunkData"][e]["id"]["X"]*stuff["chunkSize"]["X"]/2, stuff["chunkData"][e]["id"]["Y"]*stuff["chunkSize"]["Y"]/2, stuff["chunkData"][e]["id"]["Z"]*stuff["chunkSize"]["Z"]/2);
        }

Oops. I forgot about the mesh renderer. :slight_smile: