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;
        }
    }
}
相关推荐
AA陈超12 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-11 实现自动运行
c++·游戏·ue5·游戏引擎·虚幻
Hody9116 小时前
【XR开发系列】Unity下载与安装详细教程(UnityHub、Unity)
unity·游戏引擎·xr
程序员正茂18 小时前
在Unity3d中使用Netly开启TCP服务
unity·tcp·netly
Little丶Seven18 小时前
使用adb获取安卓模拟器日志
android·unity·adb·个人开发
雪下的新火2 天前
Blender-一个简单的水
游戏引擎·blender·特效制作·笔记分享
黄思搏2 天前
Unity坐标转换指南 - 3D与屏幕UI坐标互转
ui·3d·unity
weixin_424294673 天前
在 Unity 游戏开发中,为视频选择 VP8 还是 H.264
unity·游戏引擎
一步一个foot-print3 天前
【Unity】Light Probe 替代点光源给环境动态物体加光照
unity·游戏引擎
@LYZY3 天前
Unity 中隐藏文件规则
unity·游戏引擎·游戏程序·vr
霜绛3 天前
C#知识补充(二)——命名空间、泛型、委托和事件
开发语言·学习·unity·c#