- Home /
how to: create a four vertex, two tri square polygon in Unity Editor or Script?
The plane object has WAY to many tris/verts for what I want. And I can't manipulate the verts. Or delete some of them.
I've had that issue as well -- the plane prefab is (I believe) 10x10 verts, and also 10x10 units big. I found myself wanting a 2x2 quad at size 1x1, so I can look at the scale to see how big the thing is. I used procedural mesh creation to solve this, similar to Statements example.
Answer by Statement · Mar 07, 2011 at 05:51 PM
You can create a new Mesh like so:
using UnityEngine;
[RequireComponent(typeof(MeshRenderer)), RequireComponent(typeof(MeshFilter))]
public class CreatePlaneMeshExample : MonoBehaviour
{
void Start()
{
GetComponent<MeshFilter>().mesh = CreatePlaneMesh();
}
Mesh CreatePlaneMesh()
{
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[]
{
new Vector3( 1, 0, 1),
new Vector3( 1, 0, -1),
new Vector3(-1, 0, 1),
new Vector3(-1, 0, -1),
};
Vector2[] uv = new Vector2[]
{
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(0, 0),
};
int[] triangles = new int[]
{
0, 1, 2,
2, 1, 3,
};
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
return mesh;
}
}
If I procedurally create a $$anonymous$$esh in the editor, then turn its game object into a prefab, the mesh is gone when I next load the scene. Does that seem like expected behaviour? Should a procedural mesh be able to live in a prefab?
@$$anonymous$$rishna$$anonymous$$V: ?
@yoyo: Yeah, you need to create a new asset via AssetDatabase.CreateAsset - otherwise the mesh has no backing storage.
@yoyo The game object is one thing and the mesh is another thing. You have to "save both things" if you want a prefab with a mesh on it. Check out AssetDatabase.CreateAsset. If you dont save the mesh as well, it will be lost. This is expected behaviour. The only way a mesh could be saved without creating an asset would be for a game object in a scene to reference the mesh. If you then just save the scene, Unity will understand that the mesh belongs to the scene and serialise it into the scene itself. For prefabs you have to save the mesh yourself.
Answer by Jesse Anders · Mar 07, 2011 at 05:52 PM
You can create one in a modeling program and import it. Or, you can write code (or use existing code) to create a 'quad' mesh procedurally.
I think there's a 'create plane mesh' script on the script wiki that allows you to specify the subdivision level, so you might look for that. If you get stuck though, post back (I have a script lying around somewhere that should do just what you're wanting).
[Drat...thwarted by a complete example! :)]
[@The OP: Note however that the above code will create a mesh procedurally at run time. If you just want a mesh model or quad prefab that can be shared between scenes/game objects, that can be done as well. (I have a 'quad' prefab that was generated this way that I use for sprites and that sort of thing.)]
I do believe a neater solution would be to create a plane mesh model and import it into Unity from a modeling software though.
Sure, that works as well. The reason I did it procedurally was a) I couldn't figure out how to get Unity not to rotate/flip/re-arrange my Blender model on import (although that might have been user error), and b) I was already doing a lot of procedural mesh generation in the application, so writing a little code to generate some meshes that I needed was no big deal. But sure, one should use whatever method they find to be easiest and most straightforward.
both of these are right answers. I feel bad for having to give the answer "tick" to Statement for his code. And even more so, because I'll probably go with the import a mesh plain option, which deserves a tick in and of itself.
Answer by dentedpixel · Jan 19, 2012 at 05:30 PM
This script seems to be the most effective way to create a simple plane without having to bother with a 3d Editor:
[http://www.unifycommunity.com/wiki/index.php?title=CreatePlane][1] [1]: http://www.unifycommunity.com/wiki/index.php?title=CreatePlane
Answer by Banglemoose · Mar 22, 2015 at 07:47 PM
Old thread but the Quad is 2 polygons for anyone who comes across this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111727im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect perimetral walls of a room 1 Answer
Selecting a single polygon / face at runtime 1 Answer
Speedtree verts are insane 0 Answers
Vertex count 10 times higher in Unity 5 Answers
5 Player Models in Scene and already about 1.3 m Verts ? 2 Answers