Reference Link: http://blog.csdn.net/zuoyamin/article/details/9287507
For custom mesh, it's important to have three points:
1. Number of vertices = number of triangles +2; triangle vertex number =3* triangle number
2. The order of vertex creation is best to be created clockwise or counterclockwise, which greatly reduces the complexity of the algorithm
3. The order of vertex drawing must be drawn clockwise or counterclockwise in order to correctly draw a graph with vertices as a boundary point, and the sequence of vertex drawing reference mesh.triangles
Using unityengine;using System.Collections; [Requirecomponent (typeof (Meshfilter))] [Requirecomponent (typeof (Meshrenderer))]public class Custommesh:monobehaviour {}
Using unityengine;using system.collections;using system.collections.generic;using unityeditor;// It is better to lock the Inspector panel before use! [Customeditor (typeof (Custommesh))] public class Custommesheditor:editor {private list<vector3> vertices = new List<vector3> (); Private int[] triangles; void Onscenegui () {Custommesh Custommesh = (Custommesh) target; for (int i = 0; i < vertices. Count; i++) {Handles.positionhandle (vertices[i], quaternion.identity); Handles.label (Vertices[i], i.tostring ()); } if (vertices. Count > 1) {handles.drawpolyline (vertices). ToArray ()); Handles.drawline (vertices[vertices. Count-1], vertices[0]); The new view can only be drawn through handles in Onscenegui ()//If you want to introduce elements of the GUI, use the Begingui () and Endgui () combinations. Handles.begingui (); Guilayout.beginarea (New Rect (0, 0, 100, 100)); if (Guilayout.button ("Create point")) {vertices. ADD (custommesh.tRansform.position); } if (Guilayout.button ("Undo Point")) {vertices. RemoveAt (vertices. COUNT-1); } if (Guilayout.button ("Export Mesh")) {Mesh mesh = new mesh (); int triangleamount = vertices. Count-2; Triangles = new int[3 * Triangleamount]; Based on the number of triangles, the vertex Order (index)//order of the drawing triangle must be clockwise or counterclockwise for (int i = 0; i < Triangleamount; i++) {Triangles[3 * i] = 0;//fixed First point triangles[3 * i + 1] = i + 1; Triangles[3 * i + 2] = i + 2; } mesh.vertices = vertices. ToArray (); Mesh.triangles = triangles; Assetdatabase.createasset (Mesh, "assets/custommesh.asset"); } guilayout.endarea (); Handles.endgui (); }}
[Unity Combat] Custom Mesh