Unity选择框(魔兽争霸3)

复制代码
using UnityEngine;

public class SelectionBox : MonoBehaviour
{
    private Vector2 startPos;
    private Vector2 endPos;
    private bool isDragging = false;
    //边界的宽度
    public float thickness = 5;
    //区域颜色、边界颜色
    public Color areaColor = Color.white, borderColor = Color.white;
    Rect GetScreenRect(Vector2 start, Vector2 end)
    {
        // 确保矩形是从左上到右下
        float x = Mathf.Min(start.x, end.x), y = Mathf.Max(start.y, end.y);
        float width = Mathf.Abs(start.x - end.x), height = Mathf.Abs(start.y - end.y);
        return new Rect(x, Screen.height - y, width, height);
    }
    // 绘制矩形
    void DrawScreenRect(Rect rect, Color color)
    {
        GUI.color = color;
        GUI.DrawTexture(rect, Texture2D.whiteTexture);
        GUI.color = Color.white;
    }
    // 绘制矩形边框
    void DrawScreenRectBorder(Rect rect, float thickness, Color color)
    {
        GUI.color = color;
        GUI.DrawTexture(new Rect(rect.x, rect.y, rect.width, thickness), Texture2D.whiteTexture);                 // 上
        GUI.DrawTexture(new Rect(rect.x, rect.yMax - thickness, rect.width, thickness), Texture2D.whiteTexture);  // 下
        GUI.DrawTexture(new Rect(rect.x, rect.y, thickness, rect.height), Texture2D.whiteTexture);                // 左
        GUI.DrawTexture(new Rect(rect.xMax - thickness, rect.y, thickness, rect.height), Texture2D.whiteTexture); // 右
        GUI.color = Color.white;
    }
    public Transform target;
    // 获取框选区域内的单位
    void SelectUnits()
    {
        Rect selectionRect = GetScreenRect(startPos, endPos);
        Transform target = this.target;
        if (target != null)
        {
            Vector2 screenPos = Camera.main.WorldToScreenPoint(target.position);
            target.GetChild(0).gameObject.SetActive(selectionRect.Contains(screenPos));
            if (selectionRect.Contains(screenPos))
            {
                //在区域内
            }
            else
            {
                //不在区域内
            }
        }
    }
    void OnGUI()
    {
        if (isDragging)
        {
            Rect rect = GetScreenRect(startPos, endPos);
            DrawScreenRect(rect, areaColor);
            DrawScreenRectBorder(rect, thickness, borderColor);
        }
    }
    void Update()
    {
        // 开始拖拽
        if (Input.GetMouseButtonDown(0))
        {
            startPos = Input.mousePosition;
            Debug.Log(startPos);
            isDragging = true;
            target.GetChild(0).gameObject.SetActive(false);
        }
        // 结束拖拽
        else if (Input.GetMouseButtonUp(0))
        {
            isDragging = false;
            SelectUnits();
        }
        // 更新当前鼠标位置
        if (isDragging)
        {
            endPos = Input.mousePosition;
        }
    }
}
相关推荐
ellis197035 分钟前
toLua[七] Examples 06_LuaCoroutine2分析
unity
L X..44 分钟前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
Xeon_CC10 小时前
打开多个Unity编辑器时使用Visual Studio调试,弹出选择Unity实例窗口,但是没有实例
unity·编辑器·visual studio·调试·unity 调试
小L~~~14 小时前
2025吉比特-游戏引擎开发-一面复盘
数据结构·算法·游戏引擎
云卓SKYDROID20 小时前
无人机中继器模式技术对比
人工智能·游戏引擎·php·无人机·cocos2d·高科技·云卓科技
future_studio2 天前
聊聊 Unity(小白专享、熟悉基础编程 ... ...)
unity·游戏引擎
ellis19702 天前
toLua[六] Examples 05_LuaCoroutine分析
unity
程序员正茂2 天前
Unity3d中Tab控件的实现
ui·unity·tab·控件
三掌柜6663 天前
突破AR视觉交互边界:Unity赋能Rokid AR眼镜实现高精度图像识别与实时跟踪
unity·ar·交互
Brianna Home4 天前
Godot4.3开发2D游戏全记录
游戏·游戏引擎·godot·游戏程序·动画