效果图:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CDrawLine : MonoBehaviour {
//点数
public int pointCount = 10;
//距离---范围半径
public float radius = 10f;
private float angle;
private List<Vector3> pointList = new List<Vector3>();
private LineRenderer render;
public bool showRenderer = false;
// Use this for initialization
void Start () {
angle = 360f / pointCount;
render = this.GetComponent<LineRenderer>();
//Vector3 vect3 = transform.position + transform.forward * radius;
//Debug.Log(vect3);
}
// Update is called once per frame
void Update () {
if (showRenderer)
{
//设置圆的点数
render.SetVertexCount(pointCount + 1);
Vector3 vect3 = transform.position + transform.forward * radius;
pointList.Add(vect3);
Quaternion rotation = transform.rotation;
for (int i = 1; i < pointCount; i++)
{
//求出第i个 点的旋转 角度
Quaternion quat = Quaternion.Euler(rotation.eulerAngles.x, rotation.eulerAngles.y - (angle * i), rotation.eulerAngles.z);
vect3 = transform.position + (quat * Vector3.forward) * radius;
pointList.Add(vect3);
}
for (int j = 0; j < pointList.Count; j++)
{
//绘制线条
//Debug.DrawLine(transform.position, pointList[j], Color.green);
render.SetPosition(j, pointList[j]);
}
if (pointList.Count > 0)
render.SetPosition(pointCount, pointList[0]);
}
else
{
render.SetVertexCount(0);
}
//清空list数组
pointList.Clear();
}
}