Several ways to draw lines (grids) in Unity

1. GL line

This is a call to the low-level graphics library to draw lines. It is more efficient. It needs to be placed in the OnPostRender() function and the code is mounted on the camera. The following code draws a square.

using System.Collections.Generic;
using UnityEngine;

public class GLSquare : MonoBehaviour
{
    
    
    //网格材质
    public Material LineMat;//随意指定一个材质就行
    //网格颜色
    public Color MeshColor;
    //网格线坐标存储
    List<Vector3[]> m_linePoints = new List<Vector3[]>();

    void Start()
    {
    
    
        initPoints();
        //修改网格材质颜色
        LineMat.SetColor("_Color", MeshColor);
    }

    //一个10*10的,单位大小为1网格
    void initPoints()
    {
    
    
        for(int i = -5; i <= 5; i++)
        {
    
    
            Vector3[] rows = new Vector3[2];
            rows[0] = new Vector3(-5, 0, i);
            rows[1] = new Vector3(5, 0, i);
            m_linePoints.Add(rows);
            Vector3[] clos = new Vector3[2];
            clos[0] = new Vector3(i, 0, 5);
            clos[1] = new Vector3(i, 0, -5);
            m_linePoints.Add(clos);
        }
    }

    /// <summary>
    /// 照相机完成场景渲染后调用
    /// </summary>
    void OnPostRender()
    {
    
    
        //线条材质
        LineMat.SetPass(0);
        GL.PushMatrix();
        //线条颜色,当前材质下,该方式修改颜色无效,详情可以看官方文档
        //GL.Color(MeshColor);
        //绘制线条
        GL.Begin(GL.LINES);

        //所有线条 (两点一条线)
        for (int i = 0; i < m_linePoints.Count; i++)
        {
    
    
            GL.Vertex(m_linePoints[i][0]);
            GL.Vertex(m_linePoints[i][1]);
        }
        GL.End();
        GL.PopMatrix();
    }
}

Insert image description here

Note: The code must be mounted on the camera, and the drawn lines can only be displayed in the Game view and not in the Scene view. For
specific GL parameter details, please check the official GL related documents.

2. Use Debug.DrawLine() to draw a line

Insert image description here
It is relatively simple and mostly used for debugging. The duration parameter is particularly important. It controls the time displayed by the line. If it is not set, there will be one frame.
Using the points in the previous example, the core code for underlining is:

 void Start()
    {
    
    
        initPoints();
        //画网格
        for (int i = 0; i < m_linePoints.Count; i++)
        {
    
    
            //duration参数特别重要,控制线显示的时间,不设置,则存在一帧
            Debug.DrawLine(m_linePoints[i][0], m_linePoints[i][1], Color.green, 100, true);
        }
            
    }

You can note that this code can be mounted on any object
Insert image description here
: it is displayed in the Scene view. If Gizmos is turned on in the Game view, it can also be displayed.
For details of the specific Debug parameters, you can check the official Debug related documents.

3. Use Gizmos.DrawLine () to draw lines

The underlining code in this way needs to be placed in the OnDrawGizmos or OnDrawGizmosSelected function.

 void OnDrawGizmos()
    {
    
    
        Gizmos.color = Color.blue;
        //画网格
        for (int i = 0; i < m_linePoints.Count; i++)
        {
    
    
            Gizmos.DrawLine(m_linePoints[i][0], m_linePoints[i][1]);
        }
    }

You can note that this code can be mounted on any object
Insert image description here
: it is displayed in the Scene view. If Gizmos is turned on in the Game view, it can also be displayed.
For details of the specific Gizmos parameters, you can view the official Gizmos related documents.

4. Use LineRenderer component to draw lines

The line renderer is used to draw free-floating lines in 3D space. It is relatively flexible and can adjust the start, cutoff width, color change, etc. of the connecting line. It is used to make connecting lines, motion trajectories, etc. in 3D space. However, the line of this component is An array of consecutive points, connected in order, is similar to a one-stroke painting. It is actually not suitable for drawing grids.
Insert image description here
If you want to use it to draw a grid, you can consider the following ideas:
Please add image description
Please add image description
a continuous stroke like this should be possible. This component is mainly used for drawing various lines.
The following uses LineRenderer to implement the motion trajectory code (wasd keys control the movement of the cube and generate the corresponding trajectory):
Please add image description
See the uploaded project for the code. Project git connection
For more LineRenderer, you need to know the relevant official document parameters

Note: This article's lineRenderer dynamic line drawing seems good, I haven't looked at it in detail, so I will record it.

Guess you like

Origin blog.csdn.net/yueyie/article/details/130748811