在此记录一下使用Unity3D 的LineRender绘制线的过程,经过测试LineRender与OpenGL的GL_LINE_STRIP绘制方式一样,因此计算完点之后需要把起始点即为终点,多算一个点才算闭合。
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DrawLines: MonoBehaviour
{
public float m_radius = 1.0f;
public Material m_material;
public float m_lineWidth = 1.0f;
private List<Vector3> vPath = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
int count = 60;
for (int i=1; i<= (count+1); i++)
{
if(i == (count+1))
{
float x = Mathf.Cos(2 * Mathf.PI / count) * m_radius;
float y = transform.localPosition.y;
float z = Mathf.Sin(2 * Mathf.PI / count) * m_radius;
vPath.Add(new Vector3(x, y, z));
}
else
{
float x = Mathf.Cos(2 * Mathf.PI / count * i) * m_radius;
float y = transform.localPosition.y;
float z = Mathf.Sin(2 * Mathf.PI / count * i) * m_radius;
vPath.Add(new Vector3(x, y, z));
}
}
GameObject lineGroup = new GameObject("LineGroup");
GameObject lineObject = new GameObject("RadarLine");
LineRenderer line = lineObject.AddComponent<LineRenderer>();
line.material = m_material;
line.useWorldSpace = false;
line.positionCount = vPath.Count;
line.startWidth = m_lineWidth;
line.endWidth = m_lineWidth;
line.SetPositions(vPath.ToArray());
}
// Update is called once per frame
void Update()
{
}
}
运行一下看一下效果: