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;
        }
    }
}
相关推荐
SmalBox14 小时前
【光照】Unity中的[经验模型]
unity·渲染
萘柰奈16 小时前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
Yasin Chen16 小时前
Unity UI坐标说明
ui·unity
应用市场17 小时前
无人机姿态控制系统详解与实现
游戏引擎·cocos2d
陈言必行21 小时前
Unity 性能优化 之 编辑器创建资源优化( 工作流 | 场景 | 预制体)
unity·编辑器·游戏引擎
1uther1 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
死也不注释1 天前
【Unity UGUI 交互组件——Slider(7)】
unity·游戏引擎·交互
程序猿多布1 天前
XLua教程之热补丁技术
unity·c#·lua·xlua
SmalBox2 天前
【光照】Unity中的[光照模型]概念辨析
unity·渲染
挂科是不可能出现的2 天前
unity导入blender动画
unity·游戏引擎·blender