Unity draw lines and draw polygons

Source: Internet
Author: User

Draw line code, in fact, specify at least two points, and then give the map can, no nonsense, on the code;

usingUnityengine;usingSystem.Collections; Public classLinet_1:monobehaviour {PrivateLinerenderer Linerender; Private intlinelenght=4; PrivateVector3 V0 =NewVector3 (1.0f, 0f,0f); PrivateVector3 v1=NewVector3 (0f,1.0f, 0f); PrivateVector3 v2=NewVector3 (0f,0f,1.0f); PrivateVector3 v3=NewVector3 (1.0f, 0f,0f); //Use this for initialization    voidStart () {Linerender=getcomponent<linerenderer>();    Linerender.setvertexcount (Linelenght); }        //Update is called once per frame    voidUpdate () {linerender.setposition (0, V0); Linerender.setposition (1, v1); Linerender.setposition (2, v2); Linerender.setposition (3, V3); }}

Show results

As you can see, the main thing here is to use

Linerender. SetPosition (Index,point )
This function. The first parameter is the index of the point currently being set, starting at 0, and the second parameter is the point position. is a value of type Vector3;


Here's a look at the drawing surface, on the code:
usingUnityengine;usingSystem.Collections; Public classMeshbulider:monobehaviour {PrivateMesh Mymesh; Privatevector3[] vertices; Privatevector2[] uvpoints; //Use this for initialization    voidStart () {Mymesh=getcomponent<meshfilter>(). Mesh; Vertices=Newvector3[]{NewVector3 (5f,0f,0f),NewVector3 (0f,5f,0f),NewVector3 (0f,0f,5f),NewVector3 ( -5f,0f,0f),NewVector3 (0f,-5f,0f),NewVector3 (0f,0f,-5f)}; Uvpoints=Newvector2[]{NewVector2 (0,0),NewVector2 (0,5),NewVector2 (5,5),NewVector2 (0,0),NewVector2 (0,1),NewVector2 (1,1)}; }        //Update is called once per frame    voidUpdate () {mymesh.vertices=vertices; Mymesh.uv=uvpoints; //If the triangles attribute is not written, the triangle will not be displayed;mymesh.triangles=New int[]{0,1,2,3,4,5}; }}

The point is actually specified, and then the map is assigned, and the difference is that the point is two steps when the polygon is drawn:

First: Declare and instantiate an array of type Vector3 (this array holds each vertex of the polygon, so the number of elements in this array must also be a multiple of 3);

Second: To assign a value to the triangles property of the mesh, whose value is an array of type int, the number of elements corresponds to the declared vertex array one by one;

Run results

View Panel:

It can also be seen that the rendering of the map is also rendered from the first point, the first point is the beginning of the rendering map, corresponding to the bottom left corner of the map (this does not know that the right, now is so feel ~). ~~~

Welcome everybody to study together with me, progresses together ~

Unity draw lines and draw polygons

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.