Unity Editor Development Seven-script creation model

Source: Internet
Author: User

Describe:

In fact, this tutorial is everywhere, what I really want to do is to divide the model into n equal portions to achieve dynamic loading. But I've been searching the internet for a long time, and there's no search

To the right way, or not to write clearly, reluctant to put all the code out. Or is the cut of the terrain, most want is, t4m Brush finished straight

Then divide the model into n points, so you can only bitter from the most basic of the mesh to learn it.

usingSystem.Collections;usingSystem.Collections.Generic;usingUnityengine;usingUnityeditor;usingSystem; Public classCreatmesh:editor {[MenuItem ("Tool/creatmesh")]    Static voidCreamesh () {creatvertaadtriangels (); }    //Create a vertex triangle polygon    Static voidCreatvertaadtriangels () {/*Mesh Properties * Length/Width * Number of segments*/Vector2 Size=NewVector3 (Ten,Ten);//long width (because it is flat, so XY is not high)Vector2 segment =NewVector2 (Ten,Ten);//number of XY segments                /*Mesh Component * Vertex * UV * Triangle Index * Normal*/vector3[] verters;//Vertex ArrayVector2[] UV;//UV Arrays        int[] triangles;//Triangle Index//1. Vertex array Assignment        intCountvert = Mathf.floortoint ((segment.x +1) * (Segment.y +1));//convert the total number of vertices to int type based on the length and width        floatW = size.x/segment.x;//calculate the width of each segment        floath = size.y/segment.y;//calculate the length of each segment        intINDEX01 =0; Verters=NewVector3[countvert]; UV=NewVector2[countvert];  for(inti =0; I < Segment.y +1; i++)        {             for(intj =0; J < Segment.x +1; J + +) {verters[index01]=NewVector3 (J * W,0, I * h);//Create each point (distance by number of segments of XY) and assign values to the vertex array                                             floatu = j/segment.x;//The position of each point above u                floatv = i/segment.y;//The position of each point above vUV[INDEX01]=NewVector2 (U, v);//add Uvs from each point to the UV arrayindex01++; }} vector2[] Uv02=NewVector2[] {NewVector2 (0,0),NewVector2 (0.5f,0),NewVector2 (1,0),NewVector2 (0,1),NewVector2 (0.5f,1),NewVector2 (1,1), }; //3. Triangles        intCountverttri = Mathf.floortoint (segment.x * SEGMENT.Y *6);//total number of triangles: two triangles per 1*1 segment two triangles have 6 points (containing two common points), and all the total points are the number of segments *6        intindex02 =0; Triangles=New int[Countverttri];  for(inti =0; i < Segment.y; i++)        {             for(intj =0; J < Segment.x; J + +)            {                //Clockwise                intRole = Mathf.floortoint (segment.x) +1;//number of X-directional vertices                intSelf = j + (I * role);//i=0 j=0 When the No. 0 row No. 0 point i=0 j=1 No. 0 line 1th Point                intNext = j + ((i +1) * role);//i=0 j=0 When the 1th row No. 0 point i=0 j=1 1th line 1th Point//First Square//Clockwise//First TriangleTRIANGLES[INDEX02] = self;//i=0 j=0, the No. 0 line, 0 points .TRIANGLES[INDEX02 +1] = next;//i=0 j=0, the 1th line, 0 points .TRIANGLES[INDEX02 +2] = next +1;//i=0 j=0, the 1th line, 1 points .//a second triangleTRIANGLES[INDEX02 +3] = self;//i=0 j=0, the No. 0 line, 0 points .TRIANGLES[INDEX02 +4] = next +1;//i=0 j=0, the 1th line, 1 points .TRIANGLES[INDEX02 +5] = self +1;//i=0 j=0, the No. 0 line, 1 points.
You can sort 6 points at a time, and two triangles into a square .//with the addition of J Index, all squares of the first line are drawn as I increments by one lineindex02 + =6; } } //Rendering//create a new mesh and assign a value to its propertiesMesh me =NewMesh (); Me.vertices= Verters;//Mesh vertex AssignmentMe.triangles = triangles;//Mesh Triangle Index AssignmentME.UV =UV; Me. Recalculatenormals (); //automatically add normal information//new material BallMaterial ma =NewMaterial (Shader.find ("Diffuse")); //Create a new model and assign mesh and material to himGameobject M_gameobject =NewGameobject ("M_newobj");//Create a new GameobjectM_gameobject.addcomponent<meshfilter> (). Sharedmesh = Me;//Adding Meshfilter ComponentsM_gameobject.addcomponent<meshrenderer> (). sharedmaterial = ma;//Adding Meshrenderer Components }}

Next article does not eat not to drink must make an automatic division out

Unity Editor Development Seven-script creation model

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.