[Unity Combat] Custom Mesh

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.