LineRenderer画线功能很简单,在一个空物体上添加LineRenderer组件,然后主要使用LineRenderer.SetPosition(0, StartPos)和 LineRenderer.SetPosition(1, EndPos);两句话画线,LineRenderer.startWidth = Width和LineRenderer.endWidth = Width;这两句话改变线条的宽度。
有一点要注意,之前看到谁的脚本使用代码添加材质,在编辑器下可以画线,但是打包发布之后就会发现画线不能使用(当时还把我找了好长时间)。
这样一来我就写上完整的画线脚本就出来了:
/// <summary>
/// 画线,通视
/// </summary>
/// <param name="pos"></param>
private void DrawLine(Vector3 pos)
{
//Debug.Log("huaxian");
if (!DrawCamera.gameObject.activeSelf)
{
DrawCamera.gameObject.SetActive(true);
}
//起点设置
// line.material = new Material(Shader.Find("Particles/Additive"));
line.enabled = true;
text.enabled = true;
line.startWidth = Width;
line.SetPosition(0, StartPos);
line.endWidth = Width;
//终点设置
EndPos = new Vector3(pos.x, pos.y+offset, pos.z);
line.SetPosition(1, EndPos);
line.startWidth = Width;
line.endWidth = Width;
//距离以及提示设置
float Distance = Vector3.Distance(StartPos, EndPos);
message += "text:";
text.text = "距离:" + Mathf.Round(Distance).ToString() + "米";
text.transform.position = Camera.main.WorldToScreenPoint(GetBetweenPoint(StartPos, EndPos, 0.5f));
//摄像机位置方向设置
//DrawCamera.enabled = true;
DrawCamera.transform.position = StartPos;
DrawCamera.transform