Unity画线
using System.Collections.Generic;
using UnityEngine;
/// <summary>物体绘制线</summary>
public class ObjectDrawline : MonoBehaviour
{
#region 数据申明
private LineRenderer lineRender=null;
private List<Vector3> linePoints = new List<Vector3>();
private List<GameObject> targetList = new List<GameObject>();
private bool isDisplay = false;
#endregion
#region Untiy函数
private void Update()
{
if (isDisplay && lineRender != null) UpdatPosition();
}
#endregion
#region 公开函数
public void Create(bool isDisplay = false, float lineSize=1, Material material=null)
{
this.isDisplay = isDisplay;
lineRender = gameObject.AddComponent<LineRenderer>();
Set(lineSize, material);
}
public void Set(float lineSize, Material material=null)
{
if (lineRender == null) return;
lineRender.startWidth = lineSize;
lineRender.endWidth = lineSize;
lineRender.material = material;
}
public void Show()
{
if (lineRender == null) return;
isDisplay = true;
lineRender.enabled = isDisplay;
}
public void Hid()
{
if (lineRender == null) return;
isDisplay = false;
lineRender.enabled = isDisplay;
}
public void Clear()
{
linePoints.Clear();
targetList.Clear();
}
public void Add(GameObject target)
{
targetList.Add(target);
}
public void Remove(GameObject target)
{
targetList.Remove(target);
}
public void Delete()
{
Destroy(gameObject);
}
#endregion
#region 私有函数
private void UpdatPosition()
{
if (targetList == null || targetList.Count < 2) return;
linePoints.Clear();
for (int i = 0; i < targetList.Count; i++)
{
if (targetList[i] != null) linePoints.Add(targetList[i].transform.position);
}
lineRender.SetPositions(linePoints.ToArray());
}
#endregion
}
Unity QQ交流群:299412191 欢迎对Unity感兴趣的同学加入.