Unity 鼠标框选生成绘制矩形区域

鼠标拖动的同时绘制一块同等大小的区域:如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 通过鼠标框选绘制矩形区域
/// </summary>
/// 

public enum MouseType
{
    left = 0,
    right = 1,
    middle = 2
}
public class DrawRectangleArea : MonoBehaviour
{

   

    bool _canAddArea = true;

    /// <summary>
    /// 是否可以绘制
    /// </summary>
    public bool canAddArea
    {
        set { _canAddArea = value; }
        get { return _canAddArea; }
    }
     bool _isDraw = false;

    [SerializeField]
    Transform areaCube;
   
     Vector3 leftTopPos = Vector3.zero;
     Vector3 rightBottomPos = Vector3.zero;


    /// <summary>
    /// 绘制区域所在的层级
    /// </summary>
    [SerializeField]
    LayerMask maskLayer = 0;

    /// <summary>
    /// 设置鼠标哪个按键绘制
    /// </summary>
    [SerializeField]
    MouseType _mouseType = MouseType.right;

    public MouseType mouseType { set { _mouseType = value; } }

    void AddArea()
    {
        if (_canAddArea)
        {
           
            if (Input.GetMouseButtonDown((int)_mouseType))
            {
                // 从相机位置发射一条射线经过屏幕上的鼠标点击位置
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                // 声明一个射线碰撞信息类
                RaycastHit hit;
                // 进行碰撞检测           
                if (Physics.Raycast(ray, out hit, Mathf.Infinity, maskLayer, QueryTriggerInteraction.Ignore))
                {

                    _isDraw = true;
                    leftTopPos = hit.point;

                }
            }
            if (Input.GetMouseButton((int)_mouseType))
            {
                if (_isDraw)
                {
                    // 从相机位置发射一条射线经过屏幕上的鼠标点击位置
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    // 声明一个射线碰撞信息类
                    RaycastHit hit;
                    // 进行碰撞检测
                    if (Physics.Raycast(ray, out hit, Mathf.Infinity, maskLayer, QueryTriggerInteraction.Ignore))
                    {

                        rightBottomPos = hit.point;
                        Vector3 offset = leftTopPos + rightBottomPos;
                        Vector3 scale = rightBottomPos - leftTopPos;
                        areaCube.position = new Vector3(offset.x * 0.5f, leftTopPos.y + 0.5f, offset.z * 0.5f);
                        areaCube.localScale = new Vector3(Mathf.Abs(scale.x), 1, Mathf.Abs(scale.z));

                    }

                }

            }

            if (Input.GetMouseButtonUp((int)_mouseType))
            {
                _isDraw = false;
            }
        }
    }
    // Update is called once per frame
    void Update()
    {

        AddArea();
    }
}

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现鼠标绘制矩形的方法可以参考以下代码: ```csharp public class DrawRectangle : MonoBehaviour { private Vector2 startPosition; private Vector2 endPosition; private bool isDrawing; private void Update() { if (Input.GetMouseButtonDown(0)) { startPosition = Input.mousePosition; isDrawing = true; } else if (Input.GetMouseButtonUp(0)) { isDrawing = false; // 在这里获取框选的物体并改变颜色 ChangeSelectedObjectsColor(); } if (isDrawing) { endPosition = Input.mousePosition; } } private void OnGUI() { if (isDrawing) { Rect rect = new Rect(startPosition.x, Screen.height - startPosition.y, endPosition.x - startPosition.x, -(endPosition.y - startPosition.y)); GUI.Box(rect, ""); } } private void ChangeSelectedObjectsColor() { // 获取屏幕坐标系下的开始点和结束点 Vector2 screenStartPosition = Camera.main.ScreenToWorldPoint(startPosition); Vector2 screenEndPosition = Camera.main.ScreenToWorldPoint(endPosition); // 获取框选区域的边界框 Bounds selectionBounds = new Bounds((screenStartPosition + screenEndPosition) / 2f, new Vector3(Mathf.Abs(screenEndPosition.x - screenStartPosition.x), Mathf.Abs(screenEndPosition.y - screenStartPosition.y), 1f)); // 获取所有被框选的物体 Collider2D[] selectedColliders = Physics2D.OverlapBoxAll(selectionBounds.center, selectionBounds.size, 0f); // 改变被框选物体的颜色 foreach (Collider2D selectedCollider in selectedColliders) { selectedCollider.GetComponent<Renderer>().material.color = Color.red; } } } ``` 上述代码中,首先通过监听鼠标左键按下和抬起的事件来获取框选的起点和终点,然后通过 OnGUI() 函数来绘制矩形框。在鼠标左键抬起的事件中,通过获取框选区域的边界框来获取所有被框选的物体,然后将它们的颜色改变为红色即可。注意,在获取框选区域的边界框时需要将屏幕坐标系转换为世界坐标系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值