if (Input.GetMouseButtonDown(0))
{
num++;
IsDraw = true;
//GameObjectTool.AddChild使用了工具类,生成一个新物体,并设置父物体
tempLine = GameObjectTool.AddChild(lineParent, mLine);
tempLine.transform.GetComponent<LineRenderer>().sortingOrder = num;
}
if (Input.GetMouseButtonUp(0))
{
IsDraw = false;
}
if (IsDraw)
{
screenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 2);
worldPos = mMainCamera.ScreenToWorldPoint(screenPoint); //屏幕坐标转世界坐标
//Vector3 v = new Vector3(worldPos.x, worldPos.y, MainCanvas.transform.position.z);
CopyDrawLine dl = tempLine.GetOrAddComponent<CopyDrawLine>(); //绘制每条线段的逻辑在DrawLine类里
dl.points.Add(worldPos);
dl.lr.startColor = Color.red;
dl.lr.endColor = Color.red;
dl.lr.startWidth = 0.03f;
dl.lr.endWidth = 0.03f;
dl.Draw();
}
public class CopyDrawLine : MonoBehaviour
{
public LineRenderer lr;
public List<Vector3> points;
private void Awake()
{
lr = GetComponent<LineRenderer>();
points = new List<Vector3>();
}
public void Draw()
{
lr.positionCount = points.Count;
for (int i = 0; i < lr.positionCount; i++)
{
lr.SetPosition(i, points[i]);
}
}
}