Create an empty Gameobject, add mesh filter and mesh renderer two component, and add a script createmeshscript:
Using Unityengine;
Using System.Collections;
[Executeineditmode]
public class Createmeshscript:monobehaviour {
void Awake () {
Gameobject.getcomponent<meshfilter> (). Mesh = Createmesh (n);
}
Mesh Createmesh (float width, float height)
{
Mesh m = new mesh ();
M.name = "Scriptedmesh";
Note:unity is left-hand system
M.vertices = new vector3[] {
New Vector3 (-width,-height, 0),
New Vector3 (-width, height, 0),
New Vector3 (width, height, 0),
New Vector3 (width,-height, 0)
};
M.UV = new vector2[] {
New Vector2 (0, 0),
New Vector2 (0, 1),
New Vector2 (1, 1),
New Vector2 (1, 0)
};
M.triangles = new int[] {0, 1, 2, 0, 2, 3};
M.recalculatenormals ();
M.recalculatebounds ();
return m;
}
}
You can now see that the mesh Filter->mesh in inspector is displayed as Scriptedmesh.
The mesh created at this time has no material, is displayed in purple, a new material is dragged onto the mesh renderer->materials->element 0 to complete the mesh creation.
Because [Executeineditmode] is added to the script, you can see the newly created mesh without running directly in the scene window.
It is also important to note that:
1,unity is the coordinate system is the left-hand line, if you do not pay attention to this point of the vertex order (normal) is reversed, may be drawn from the back of the object can not be seen.
2,mesh.vertices to use local coordinates.
Unity, creating mesh with scripts