public class LineCreater : BaseCreater
{
private bool isDrawing;//是否在画线
private LineRenderer wallLine;
private Vector3 StartPosition, EndPosition;
private float wallWidth = 0.5f;
private float wallHeight = 3f;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current != null && EventSystem.current.IsPointerOverGameObject())
{
//判断是否点击UI
return;
}
var inputPosition = GetMousePosition();
StartPosition = inputPosition;
CreateLine(StartPosition);
}
if (Input.GetMouseButtonUp(0))
{
EndPosition = GetMousePosition();
if ((EndPosition - StartPosition).sqrMagnitude > 1 * 1)
{
DrawLineFinished();
}
else
{
Destroy(wallLine.gameObject);
}
isDrawing = false;
}
if (isDrawing)
{
wallLine.SetPosition(1, GetMousePosition());
}
}
/// <summary>
/// 获取鼠标点击的坐标
/// </summary>
/// <returns></returns>
private Vector3 GetMousePosition()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
return hit.point - new Vector3(0, 0, 0.01f);
}
return Vector3.zero;
}
/// <summary>
/// 画线完成
/// </summary>
private void DrawLineFinished()
{
GetVertices();
StartPosition = Vector3.zero;
EndPosition = Vector3.zero;
}
/// <summary>
/// 创建线
/// </summary>
void CreateLine(Vector3 start)
{
GameObject wall = new GameObject("wall");
wall.transform.SetParent(transform);
wallLine = wall.AddComponent<LineRenderer>();
wallLine.startWidth = 0.05f;
wallLine.endWidth = 0.05f;
wallLine.SetPosition(0, start);
isDrawing = true;
}
}
版权声明:本文为u010197227原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。