unity 框选

该代码段展示了如何在Unity中实现矩形框选功能。`RectRenderController`类包含鼠标交互逻辑,用于在屏幕上绘制并更新选择框。当鼠标按下时记录起点,松开时记录终点,并通过`LineRenderer`组件绘制四边形框。更新过程中,根据鼠标位置实时更新框选区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/****************************************************
    文件:RectRenderController.cs
	作者:Edision
    邮箱: 424054763@qq.com
    日期:#CreateTime#
	功能:框选控制
*****************************************************/

using UnityEngine;

public class RectRenderController : MonoBehaviour
{
    /// <summary>
    /// 鼠标左键是否按下
    /// </summary>
    private bool isMouseDown;

    /// <summary>
    /// 画线工具
    /// </summary>
    private LineRenderer lineRenderer;

    /// <summary>
    /// 框的起始点,即按下鼠标左键时指针的位置
    /// </summary>
    private Vector3 startPoint;

    /// <summary>
    /// 在拖移过程中,玩家鼠标指针所在的实时位置
    /// </summary>
    private Vector3 currentPoint;

    /// <summary>
    /// 框的终止点,即放开鼠标左键时指针的位置
    /// </summary>
    private Vector3 endPoint;


    private void Awake()
    {
        isMouseDown = false;
        lineRenderer = GetComponent<LineRenderer>();
        if (lineRenderer==null)
        {
            lineRenderer = this.gameObject.AddComponent<LineRenderer>();
        }
        lineRenderer.loop = true;
        lineRenderer.useWorldSpace = true;
        lineRenderer.SetWidth(0.01f, 0.01f);
    }


    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPoint = Input.mousePosition;

            isMouseDown = true;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isMouseDown = false;
            lineRenderer.positionCount = 0;
            endPoint = Input.mousePosition;
        }
        RectRender();
    }

    private float temp_Z = 5f;
    private Vector3 temp_RightUpPos;
    private Vector3 temp_LeftDownPos;

    /// <summary>
    /// 框选绘制
    /// </summary>
    private void RectRender()
    {
        if (isMouseDown)
        {
            currentPoint = Input.mousePosition;
            startPoint.z = temp_Z;
            currentPoint.z = temp_Z;

            SetV3(ref temp_RightUpPos, currentPoint.x, startPoint.y, temp_Z);
            SetV3(ref temp_LeftDownPos, startPoint.x, currentPoint.y, temp_Z);

            lineRenderer.positionCount = 4;
            lineRenderer.SetPosition(0, Camera.main.ScreenToWorldPoint(startPoint));
            lineRenderer.SetPosition(1, Camera.main.ScreenToWorldPoint(temp_RightUpPos));
            lineRenderer.SetPosition(2, Camera.main.ScreenToWorldPoint(currentPoint));
            lineRenderer.SetPosition(3, Camera.main.ScreenToWorldPoint(temp_LeftDownPos));

        }
    }

    private void SetV3(ref Vector3 v3, float x, float y, float z)
    {
        v3.x = x;
        v3.y = y;
        v3.z = z;
    }

    private void OnDestroy()
    {
        isMouseDown = false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值