public class Controller : MonoBehaviour
{
//划线
public float z;
private LineRenderer lineRenderer;
private bool isMouseDown;
private Vector3 leftUpPoint;
private Vector3 rightUpPoint;
private Vector3 leftDownPoint;
private Vector3 rightDownPoint;
// Start is called before the first frame update
void Start()
{
lineRenderer = this.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
SelSoldier();
}
/// <summary>
/// 划线 框选士兵
/// </summary>
private void SelSoldier()
{
if (Input.GetMouseButtonDown(0))
{
isMouseDown = true;
leftUpPoint = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isMouseDown = false;
//取消绘制
lineRenderer.positionCount = 0;
}
if (isMouseDown)
{
rightDownPoint = Input.mousePosition;
leftUpPoint.z = z;
rightDownPoint.z = z;
leftDownPoint = new Vector3(leftUpPoint.x,rightDownPoint.y,z);
rightUpPoint = new Vector3(rightDownPoint.x,leftUpPoint.y,z);
lineRenderer.positionCount = 4;
lineRenderer.SetPosition(0,Camera.main.ScreenToWorldPoint(leftUpPoint));
lineRenderer.SetPosition(1,Camera.main.ScreenToWorldPoint(rightUpPoint));
lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(rightDownPoint));
lineRenderer.SetPosition(3,Camera.main.ScreenToWorldPoint(leftDownPoint));
}
}
}
LineRenderer屏幕绘制
最新推荐文章于 2024-11-07 09:11:54 发布
文章描述了一个在Unity游戏开发中的C#代码片段,展示了如何在Controller组件中使用LineRenderer进行鼠标点击划线,用于框选屏幕上的士兵。更新函数SelSoldier()处理了鼠标按下和抬起事件,更新LineRenderer的位置以显示选择区域。
摘要由CSDN通过智能技术生成