Creating prefab via Unity Editor button click with procedural plane mesh

Hi guys, I seem to be having some trouble creating the mesh for the prefabs I am trying to create when I click a button which is part of my custom editor window. This is the createPlane method I am using:

static Mesh createTexturePlaneMesh(Rect frame, out Mesh m)
	{
		Debug.Log("frame"+frame);
		m = new Mesh();
		m.name = "Plane Mesh";
		
		m.vertices = new Vector3[4];
		m.vertices[0] =new Vector3(-frame.width/2.0f, -frame.height/2.0f, 0.01f);
		m.vertices[1] =new Vector3(frame.width/2.0f, -frame.height/2.0f, 0.01f);
		m.vertices[2] =new Vector3(frame.width/2.0f, frame.height/2.0f, 0.01f);
		m.vertices[3] =new Vector3(-frame.width/2.0f, frame.height/2.0f, 0.01f);
		
		Debug.Log("Vert1"+m.vertices[0]);

		
		m.uv = new Vector2[4];
		m.uv[0] = new Vector2(0,0);
		m.uv[1] = new Vector2(0,1);
		m.uv[2] = new Vector2(1,1);
		m.uv[3] = new Vector2(1,0);
		
		m.triangles = new int[6];
		m.triangles[0] = 0;
		m.triangles[1] = 1;
		m.triangles[2] = 2;
		m.triangles[3] = 0;
		m.triangles[4] = 2;
		m.triangles[5] = 3;

		m.RecalculateNormals();
		
		return m;

Frame is taken from a file which is loaded in, and it is loaded correctly as the first debug statement correctly prints the value I was expecting for each of the Rect’s properties. However, the 2nd debug log returns a (0,0,0) value, which is incorrect.

My prefab does get created, and it has each of the following components: MeshRenderer, MeshFilter, MeshCollider.

I then use : g.GetComponent<MeshFilter>().mesh = planeMesh; where planeMesh is the value returned from my function to set the Mesh for the prefab. In the inspector however, the MeshFilter component section states MeshFilter [None].

I feel I am doing something stupid. Thanks for any help you give.

First of the out parameter is like a reference to an object, you should not return it.

Second of all, all arrays changes when you change the vertices array.
So normally you save all of them in a new array and then:

m.vertices = newArray[ ], or if you are using a list, m.vertices = list.ToArray();
You do the same for uvs, colors, normals and so on.

Thanks for the reply, ye I meant to edit out that return and change the return type to void, but thanks for that anyway. As for the second part of your answer, I am not so sure how to create the array like that in c#.
I think you mean doing like: m.vertices = new Vector3[new Vector3(x,x,x), new Vector3(x,x,x)…)…am I right? If so, I cannot figure out the syntax to do it like that. Thanks for the reply.

Hi sorry for the shameless bump, but I do not really understand what you mean BFGames.
"Second of all, all arrays changes when you change the vertices array. " What do you mean here?

        Vector3[] newArray = new Vector3[4];

        newArray [0] =new Vector3(-frame.width/2.0f, -frame.height/2.0f, 0.01f);

        newArray [1] =new Vector3(frame.width/2.0f, -frame.height/2.0f, 0.01f);

        newArray [2] =new Vector3(frame.width/2.0f, frame.height/2.0f, 0.01f);

        newArray[3] =new Vector3(-frame.width/2.0f, frame.height/2.0f, 0.01f);

        m.vertices = newArray;

Thanks again for the quick reply. However, I cannot see how that is any different to my code :confused: except it goes a longer route. Can you explain why my version is wrong?

I solved my initial problem. I needed to save the created mesh as an asset so the reference would remain for the prefab.