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
- Using Unityengine;
- Using System.Collections;
- Using System.Threading;
- Public class Main:monobehaviour {
- //Game object, here is the segment object
- private Gameobject Linerendergameobject;
- //Line renderer
- private Linerenderer Linerenderer;
- //Set the number of segments to indicate that a curve consists of several segments
- private int linelength = 4;
- //Record 4 points to connect a line segment through the points in these 4 three-dimensional worlds
- private Vector3 V0 = new Vector3 (1.0f,0.0f,0.0f);
- private Vector3 v1 = new Vector3 (0.0f,1.0f,0.0f);
- Private Vector3 v2 = new Vector3 (0.0f,0.0f,1.0f);
- private Vector3 v3 = new Vector3 (1.0f,0.0f,0.0f);
- void Start () {
- //through the name of the object you created earlier, you can get this object in other classes,
- //Get the Line object here in Main.cs
- Linerendergameobject = Gameobject.find ("line");
- //Through the game object, Getcomponent method incoming Linerenderer
- //Is the Renderer property that was added to the line game object before
- //have this object to render line segments for the game world
- Linerenderer = (linerenderer) linerendergameobject.getcomponent ("Linerenderer");
- //Set segment length, this value need and draw line 3D point number want to wait
- //Otherwise it will throw an exception ~ ~
- Linerenderer.setvertexcount (linelength);
- }
- void Update () {
- //To set points in the game update
- //link this curve to the point.
- //The first parameter is the ID of a point
- ///The second parameter is a 3D coordinate of the point
- //id The same thing, it's marked as a line segment.
- //So the pot friends need to pay attention!
- Linerenderer.setposition (0, V0);
- Linerenderer.setposition (1, v1);
- Linerenderer.setposition (2, V2);
- Linerenderer.setposition (3, V3);
- }
- }
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