Unity 编辑器格子工具

 

using UnityEditor;
using UnityEngine;

public class CustomGridWindow3 : EditorWindow
{
    private Texture2D customBackgroundTexture;
    private Vector2 gridSize = new Vector2(10, 10);
    private Color[] cellColors;
    private bool isSelecting = false;
    private Vector2 selectionStart;
    private Vector2 selectionEnd;

    [MenuItem("工具/格子3")]
    public static void ShowWindow()
    {
        GetWindow<CustomGridWindow3>("Custom Grid");
    }

    private void OnEnable()
    {
        InitializeCellColors();
    }

    private void InitializeCellColors()
    {
        cellColors = new Color[(int)(gridSize.x * gridSize.y)];
        for (int i = 0; i < cellColors.Length; i++)
        {
            cellColors[i] = Color.clear; // 初始颜色为透明
        }
    }

    private void OnGUI()
    {
        customBackgroundTexture = EditorGUILayout.ObjectField("Custom Background", customBackgroundTexture, typeof(Texture2D), false) as Texture2D;

        if (customBackgroundTexture != null)
        {
            Rect gridRect = GUILayoutUtility.GetRect(position.width, position.width / gridSize.x * gridSize.y);

            Event e = Event.current;

            // 处理鼠标点击事件
            if (e.isMouse && gridRect.Contains(e.mousePosition))
            {
                Vector2 mousePos = e.mousePosition - gridRect.position;
                int cellX = Mathf.FloorToInt(mousePos.x / gridRect.width * gridSize.x);
                int cellY = Mathf.FloorToInt(mousePos.y / gridRect.height * gridSize.y);
                int cellIndex = (int)(cellY * gridSize.x + cellX);

                if (e.type == EventType.MouseDown && e.button == 0)
                {
                    isSelecting = true;
                    selectionStart = new Vector2(cellX, cellY);//按下鼠标是起点
                }
                else if (e.type == EventType.MouseUp && e.button == 0)
                {
                    isSelecting = false;
                    selectionEnd = new Vector2(cellX, cellY);//松开鼠标是终点
                    ApplySelection();
                }
                else if (isSelecting && e.type == EventType.MouseDrag)
                {
                    selectionEnd = new Vector2(cellX, cellY);//拖拽
                }
            }

            // 绘制背景图
            GUI.DrawTexture(gridRect, customBackgroundTexture, ScaleMode.ScaleToFit);

            // 绘制网格边框
            Handles.color = Color.white;
            Handles.DrawWireDisc(gridRect.center, Vector3.forward, gridRect.width / 2f);

            // 绘制网格线
            Handles.color = Color.black;
            float cellWidth = gridRect.width / gridSize.x;
            float cellHeight = gridRect.height / gridSize.y;

            for (int x = 1; x < gridSize.x; x++)
            {
                float xPos = gridRect.x + x * cellWidth;
                Handles.DrawLine(new Vector3(xPos, gridRect.y), new Vector3(xPos, gridRect.y + gridRect.height));
            }

            for (int y = 1; y < gridSize.y; y++)
            {
                float yPos = gridRect.y + y * cellHeight;
                Handles.DrawLine(new Vector3(gridRect.x, yPos), new Vector3(gridRect.x + gridRect.width, yPos));
            }

            // 绘制单元格颜色
            for (int y = 0; y < gridSize.y; y++)
            {
                for (int x = 0; x < gridSize.x; x++)
                {
                    int cellIndex = (int)(y * gridSize.x + x);
                    Rect cellRect = new Rect(gridRect.x + x * cellWidth, gridRect.y + y * cellHeight, cellWidth, cellHeight);
                    GUI.color = cellColors[cellIndex];
                    GUI.DrawTexture(cellRect, EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill);
                }
            }

            // 绘制选择框
            if (isSelecting)
            {
                Vector2 min = Vector2.Min(selectionStart, selectionEnd);
                Vector2 max = Vector2.Max(selectionStart, selectionEnd);
                Rect selectionRect = new Rect(min.x * cellWidth, min.y * cellHeight, (max.x - min.x + 1) * cellWidth, (max.y - min.y + 1) * cellHeight);
                GUI.color = new Color(0, 0, 1, 0.2f); // 浅蓝色,带透明度
                GUI.DrawTexture(selectionRect, EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill);
            }

            GUI.color = Color.white;

            // 清除选择的按钮
            if (GUILayout.Button("所选格子"))
            {
                PrintSelectionData();
            }
        }
    }

    private Color selectColor = new Color(0, 0, 1, 0.5f);
    
    private void ApplySelection()
    {
        for (int y = (int)selectionStart.y; y <= (int)selectionEnd.y; y++)
        {
            for (int x = (int)selectionStart.x; x <= (int)selectionEnd.x; x++)
            {
                int cellIndex = (int)(y * gridSize.x + x);
                if (cellColors[cellIndex] == selectColor) {
                    cellColors[cellIndex] = Color.clear;// 已经是蓝色设置为灰色
                } else {
                    cellColors[cellIndex] = selectColor; // 设置为蓝色,带透明度
                }
            }
        }
        Repaint();
    }

    private void PrintSelectionData()
    {
        Debug.Log("打印所选格子");
        for (int i = 0; i < cellColors.Length; i++) {
            if (cellColors[i] == selectColor) {
                Debug.Log(i + "   x:" + i % (int)gridSize.x + "   y" + i / (int)gridSize.y);
            }
        }
    }
}

 

posted @ 2023-11-08 16:19  三页菌  阅读(44)  评论(0编辑  收藏  举报