Unity3d-mesh Create

Source: Internet
Author: User

Mesh needs to be created in code.

Using unityengine;using system.collections;using Unityeditor;public class Createmesh:monobehaviour {//Vertex collection Priva    Te vector3[] vertices = new vector3[constnumber.pointsum]; Triangle index Altogether (constnumber.xlength-1) * (constnumber.ylength-1) of rectangles, each rectangle 6 points private int[] triangles = new INT[6 * (Con    STNUMBER.XLENGTH-1) * (constnumber.ylength-1)]; Private color[] Colors = new color[constnumber.pointsum];//Use this for initializationvoid Start () {Createme    Shfun ();}        public void Createmeshfun () {Mesh mesh = new Mesh ();        Mesh = Createmeshobject ();        Assetdatabase.createasset (Mesh, "assets/" + "pollutant" + ". Asset");    Print the path of the new resource//Debug.Log (Assetdatabase.getassetpath (mesh));        } Private Mesh Createmeshobject () {Mesh mesh = new mesh ();        Initialize vertex position initvertexpos ();        Triangle index inittriangles ();                Initialize color initcolors ();        Vertex assignment mesh.vertices = vertices;Index assignment mesh.triangles = triangles;        To vertex coloring mesh.colors = colors; Recalculates the normal mesh of the mesh.        Recalculatenormals ();    return mesh;        }//initialize vertex position private void Initvertexpos () {int currentindex = 0;            for (int i = 0, i < constnumber.ylength; i++) {for (int j = 0; J < Constnumber.xlength; J + +)                {Vertices[currentindex] = new Vector3 (j, 0, I);            currentindex++;        }}}//initialize triangle index private void Inittriangles () {//Represents the current index value of the Triangl array, each place a value in the array, Currentindex increases by 1                int currentindex = 0; for (int i = 0, i < constnumber.ylength-1; i++) {for (int j = 0; J < Constnumber.xlength-1; j + +) {//Draw the upper left corner triangle triangles[currentindex++] = (i + 0) * Constnumber.xlength + (                j + 0);           triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 0);     triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 1);                Draw clockwise triangle triangles[currentindex++] = (i + 0) * Constnumber.xlength + (j + 0);                triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 1);            triangles[currentindex++] = (i + 0) * Constnumber.xlength + (j + 1);  }}}//Initialize color private void Initcolors () {for (int i = 0; i < constnumber.pointsum;            i++) {Color color = new Color (0, 0, 0);        Colors[i] = color; }    }}

Will grow into a rectangular mesh with a xlength width of ylength


OK, drag in, hierarchy panel, get Gameobject


Mesh creation is complete.




Unity3d-mesh Create

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.