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();
    }
}

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 07:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 07:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 07:42:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 07:42:02       20 阅读

热门阅读

  1. 谈谈 Redis 除了作缓存之外还能干什么

    2023-12-30 07:42:02       33 阅读
  2. JWT+Redis 实现接口 Token 校验

    2023-12-30 07:42:02       40 阅读
  3. 知识笔记(六十一)———Vue生命周期

    2023-12-30 07:42:02       42 阅读
  4. docker启动mysql并映射数据目录、含备份脚本

    2023-12-30 07:42:02       42 阅读
  5. DjangoRestFramework概括

    2023-12-30 07:42:02       41 阅读
  6. dCardAlarmController required a single bean, but 2 were found:

    2023-12-30 07:42:02       36 阅读
  7. Kotlin基础语法

    2023-12-30 07:42:02       37 阅读