Unity uses line renderer to draw lines

Source: Internet
Author: User

Original address: http://www.xuanyusong.com/archives/561 any irregular curve it is composed of a number of segments, in time it is circular it is also a number of segments, In other words, stitching together a number of segments is the irregular curve we need ~ Then in the 3D world we need to know X Y Z three points to determine a 3D segment.   First Use the Unity editor to add a line ~ unity---gameobject---Create an empty object, and I'll name it. Then click Component, Miscellaneous, line Renderer to add a wire renderer to the property, the lines Renderer is a very good attribute, I will explain in detail below.      create, Material create a material to do the mapping of this segment, let's take a look at some important parameters of line renderer. Cast Shadows: Whether to cast shadows. Receive Shadows: Whether to receive shadows. Materials: Set the material, here you can set up a number of materials, line is the material we created above, here I give line this material painted red color. Positions: This property is more important, it is specifically set the line segment in the 3D world point of coordinates, size set point number is 3 then there will be 3 points, Element 0   element 1 element 2     These three points determine that the curve is divided into two segments, the first is (0,0,1) to   (0,0,2), and the second is (0,0,2) to (0,0,4).   paramerters startwidth: Sets the width of the starting point  endwidth:   Sets the width of the end point, which defaults to 1, but is very wide in reality, so it is generally set to 0. Several ~  start color: Set start color: Set the end color using world coordinates   you can see that the curve is divided into two parts, the first part shorter and the second part longer. How about   ? How to draw the line is not difficult to learn, right? In the case of a known segment position we can use theThe above method to set this line, but if the position of the line is in the game during the dynamic generation of the code to go to the dynamic settings.    Create a script Main.cs bind to the camera, run the game draw line, you need to get in main to take the example of this object. This is a more important point of knowledge.   Some important ways to edit something in the editor can also be done in the code.  linerenderer.setwidth (0.1,0.1); Sets the width of the start and end points of the segment (parameter 1 is the starting point parameter 2 is the end point) linerenderer.setcolor (color.black,color.white);  sets the color of the start and end points of the segment (parameter 1 is the starting point color Parameter 2 is the end point color) linerenderer.setvertexcount (5); Sets the number of segments. Linerenderer.useworldspace = true; Whether to use the world coordinate system, corresponding to the above editor.  [csharp] view plaincopy  
  1. Using Unityengine;
  2. Using System.Collections;
  3. Using System.Threading;
  4. Public class Main:monobehaviour {
  5. //Game object, here is the segment object
  6. private Gameobject Linerendergameobject;
  7. //Line renderer
  8. private Linerenderer Linerenderer;
  9. //Set the number of segments to indicate that a curve consists of several segments
  10. private int linelength = 4;
  11. //Record 4 points to connect a line segment through the points in these 4 three-dimensional worlds
  12. private Vector3 V0 = new Vector3 (1.0f,0.0f,0.0f);
  13. private Vector3 v1 = new Vector3 (0.0f,1.0f,0.0f);
  14. Private Vector3 v2 = new Vector3 (0.0f,0.0f,1.0f);
  15. private Vector3 v3 = new Vector3 (1.0f,0.0f,0.0f);
  16. void Start () {
  17. //through the name of the object you created earlier, you can get this object in other classes,
  18. //Get the Line object here in Main.cs
  19. Linerendergameobject = Gameobject.find ("line");
  20. //Through the game object, Getcomponent method incoming Linerenderer
  21. //Is the Renderer property that was added to the line game object before
  22. //have this object to render line segments for the game world
  23. Linerenderer = (linerenderer) linerendergameobject.getcomponent ("Linerenderer");
  24. //Set segment length, this value need and draw line 3D point number want to wait
  25. //Otherwise it will throw an exception ~ ~
  26. Linerenderer.setvertexcount (linelength);
  27. }
  28. void Update () {
  29. //To set points in the game update
  30. //link this curve to the point.
  31. //The first parameter is the ID of a point
  32. ///The second parameter is a 3D coordinate of the point
  33. //id The same thing, it's marked as a line segment.
  34. //So the pot friends need to pay attention!
  35. Linerenderer.setposition (0, V0);
  36. Linerenderer.setposition (1, v1);
  37. Linerenderer.setposition (2, V2);
  38. Linerenderer.setposition (3, V3);
  39. }
  40. }
Through the above code settings, run the game, found a new triangular curve impressively reflected in our eyes, with the above method we can be combined to draw a variety of 3D game curve, here Momo use is color, you can also add a map ~

(go) Unity use line renderer to draw lines

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.