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;
        }
    }
}
相关推荐
Clank的游戏栈1 小时前
Unity多线程渲染指令队列设计与集成技术详解
windows·unity·游戏引擎
胜天半子_王二_王半仙9 小时前
godot源码编译
游戏引擎·godot
Thomas_YXQ9 小时前
Unity3D IK解算器技术分析
开发语言·搜索引擎·unity·全文检索·unity3d·lucene
Tandy12356_10 小时前
Godot开发2D冒险游戏——第二节:主角光环整起来!
游戏引擎·godot
星火撩猿17 小时前
常见游戏引擎介绍与对比
unity·ue5·游戏引擎·godot
sky_smile_Allen18 小时前
[Unity]-[UI]-[Prefab] 关于Unity UGUI 的布局及组件讲解
ui·unity·游戏引擎
虾球xz20 小时前
游戏引擎学习第244天: 完成异步纹理下载
c++·学习·游戏引擎
太妃糖耶1 天前
URP-利用矩阵在Shader中实现物体的平移和缩放
unity·矩阵
Magnum Lehar1 天前
ApophisZerg游戏引擎项目目录展示
人工智能·vscode·编辑器·游戏引擎
Tandy12356_1 天前
Godot开发2D冒险游戏——第一节:主角登场!
python·游戏引擎·godot