Unity之线性渲染器

最近再做一个光线类的小游戏，时间不太够写算法的，但是不能断对算法的研究，周六日补吧~

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 线性渲染器组类
/// 用来绘制多条线段，避免了一个渲染器发生的头粗尾短的现象
/// </summary>
public class LineRenderGroup : MonoBehaviour {
private Transform _transform;
private int size = 0;											//允许存在的线段的数量
private List <LineRenderer> segmentList; 					//线段存放的集合
private const string objectName = "Segment";
private Material lightMaterial;
private float thickness = Gloabal.lineWidth;

public float Thickness {
get {
return thickness;
}
set {
thickness = value;
SetSegmentThickness ();
}
}

void Awake () {
_transform = transform;
segmentList = new List<LineRenderer> ();
}

//设置线段顶点的方法
public void SetSegment (int index, Segment segment) {
segmentList [index].SetPosition (0, segment.startPoint);
segmentList [index].SetPosition (1, segment.endPoint);
}
//设置整体线段粗细的方法
public void SetSegmentThickness () {
for (int i = 0; i < segmentList.Count; i++) {
segmentList [i].SetWidth (thickness, thickness);
}
}
//设置光线颜色(该方法未实现)！！
public void SetLightColor (Color c) {
for (int i = 0; i < segmentList.Count; i++) {
segmentList [i].material.color = c;
}
}
//设置允许存在的顶点的数量
public void SetSegmentCount (int count) {
this.size = count;
if (size > segmentList.Count) {
}
if (size < segmentList.Count) {
RemoveSegment (segmentList.Count - size);
}
}
//添加线段的方法
for (int i = 0; i < addCount; i++) {
GameObject g = new GameObject (objectName);
g.transform.SetParent (_transform);
LineRenderer lineRender = g.AddComponent <LineRenderer> ();
lineRender.SetWidth (thickness, thickness);
lineRender.SetVertexCount (2);
lineRender.SetPosition (0, Vector3.zero);
lineRender.SetPosition (1, Vector3.zero);
lineRender.material = lightMaterial;
}
}
//移除线段的方法(采用将线段两个端点设置为同一点的方法，也可以使用对象池回收多余的LineRender)
private void RemoveSegment (int increaseCount) {
int length = segmentList.Count;
for (int i = 0; i < increaseCount; i++) {
segmentList [length - i - 1].SetPosition (0, Vector3.zero);
segmentList [length - i - 1].SetPosition (1, Vector3.zero);
}
}
}