using System;
using UnityEngine; class UILine
{
GameObject targetObj;
LineRenderer lineRenderer; //LineRenderer渲染出的线段的两个端点是3D世界中的点
int index = ;
int poinCount = ; //线段的端点数
Vector3 position; public void Start(GameObject go, Color beginColor, Color endColor, float begineWidth, float endWidth)
{
targetObj = go;
if (null != targetObj)
{
targetObj.SetActive(true);
lineRenderer = targetObj.GetComponent<LineRenderer>(); if (null == lineRenderer)
lineRenderer = targetObj.AddComponent<LineRenderer>();
} if (null != lineRenderer)
{
//颜色
lineRenderer.startColor = beginColor;
lineRenderer.endColor = endColor; //宽度
lineRenderer.startWidth = begineWidth;
lineRenderer.endWidth = endWidth;
} index = poinCount = ;
} public void Update()
{
if (Input.GetMouseButton())
{
//屏幕坐标转换为世界坐标
position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
//端点数+1
poinCount++;
//设置线段的端点数
lineRenderer.positionCount = poinCount; //连续绘制线段
while (index < poinCount)
{
//两点确定一条直线,所以我们依次绘制点就可以形成线段了
lineRenderer.SetPosition(index, position);
index++;
}
}
} public static UILine BeginLine(GameObject go, Color beginColor, Color endColor, float beginWidth, float endWidth)
{
UILine line = new UILine();
line.Start(go, beginColor, endColor, beginWidth, endWidth);
return line;
}
}
04-27 06:54