LineRenderer屏幕绘制

文章描述了一个在Unity游戏开发中的C#代码片段,展示了如何在Controller组件中使用LineRenderer进行鼠标点击划线,用于框选屏幕上的士兵。更新函数SelSoldier()处理了鼠标按下和抬起事件,更新LineRenderer的位置以显示选择区域。
摘要由CSDN通过智能技术生成
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));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值