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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值