首页 > 其他分享 >Unity 格子工具

Unity 格子工具

时间:2023-11-08 16:35:07浏览次数:40  
标签:gridRect gridSize 格子 int Vector2 private Unity new 工具

 

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);
            }
        }
    }
}

 

标签:gridRect,gridSize,格子,int,Vector2,private,Unity,new,工具
From: https://www.cnblogs.com/sanyejun/p/17817687.html

相关文章

  • Apipost-Helper:IDEA中的类postman工具
    今天给大家推荐一款IDEA插件:Apipost-Helper-2.0,写完代码IDEA内一键生成API文档,无需安装、打开任何其他软件;写完代码IDEA内一键调试,无需安装、打开任何其他软件;生成API目录树,双击即可快速定位API定义的代码…非常好用!主要包含以下功能:1、无侵入生成API文档编写完代码后,只需右键uploa......
  • Apipost-Helper:IDEA中的类postman工具
    今天给大家推荐一款IDEA插件:Apipost-Helper-2.0,写完代码IDEA内一键生成API文档,无需安装、打开任何其他软件;写完代码IDEA内一键调试,无需安装、打开任何其他软件;生成API目录树,双击即可快速定位API定义的代码…非常好用!主要包含以下功能:1、无侵入生成API文档编写完代码后,只需右键up......
  • Redis迁移工具redis-migrate-tool的使用
    背景:公司由于以前没有同意规划,导致出现好几个redis实例,现在需要整合到一起,查到还有redis-migrate-tool工具,所以把使用情况写一下,以备后用参考 一、下载编译#从git库直接拉取gitclonehttps://github.com/vipshop/redis-migrate-tool.git#CentOS安装必......
  • 使用jadx-gui反编译工具获取签名信息
     1、下载地址: github.com/skylot/jadx/releases 如图:  2、打开jadx-gui方式1:终端执行brewinstalljadx, 执行完毕后,终端再输入命令jadx-gui,即可打开jadx;方式2:解压zip包,在jadx-1.4.7文件夹下找到bin文件夹打开,双击运行jadx-gui,如图: 3、点击“打开......
  • Unity 自定义Postprocess BSC明度饱和度对比度
    前言本篇将介绍如何通过添加RenderFeature实现自定义的postprocess——调整屏幕的明度、饱和度、对比度(以下统称BSC)关于RenderFeature的基础可以看这篇https://www.cnblogs.com/chenglixue/p/17816447.htmlShaderBrightness:很简单乘以rendertargettexture即可Saturatio......
  • Unity项目开发中如何做资源加密
    Unity的游戏很容易被人反编译出来,然后再重新打包发布,把自己辛辛苦苦开发的游戏,抄写的一丝不挂。很多项目要求要做好资源加密,Unity中如何做好资源加密呢?本文給大家分享加密算法+资源打包整合思路:(1) 游戏资源加密如何选择加密算法;(2) Assetsbundle资源包的加密与解密;  ......
  • Unity架构师必备的开源库,让你3天搭建商用游戏框架
    现在Unity的相关技术已经都非常常熟了,如果你的技术能力与阅历够,搭建一个商用的游戏框架,你只需要3天的时间。今天给大家分享一个Unity老鸟3天能搭建一个自己的商用框架的几个必备的开源库,方便大家学习与使用,同时学习这些有前途的开源库也能让你在公司里面游刃有余。 1:搭建商用......
  • elasticview elk查看工具安装
    ElasticView是一款用来监控ElasticSearch状态和操作ElasticSearch索引的web可视化工具。它由golang开发而成,具有部署方便,占用内存小等优点,官网地址:http://www.elastic-view.cnElasticSearch连接树管理(更方便的切换测试/生产环境)支持权限管理支持sql转换成dsl语法更方便的重......
  • Unity HybridCLR(wolong)/huatuo系列
    Lua,ILRuntime,HybridCLR(wolong),huatuo热更对比分析 这两年,各种Unity热更新方案如雨后春笋般出来了,今天来写篇文章来对比一下Unity各大热更新方案的优缺点。目前主流的Unity热更新的方案有:  Lua系解决方案:内置一个Lua虚拟机,做好UnityEngine与C#框架的Lua导出。典型的......
  • 有哪些app开发工具支持跨平台开发?
    跨平台开发是指使用一种或多种工具和技术来开发可以在多个平台上运行的应用程序。以下是一些支持跨平台开发的APP开发工具:Flutter:由Google开发的UI工具包,可用于在iOS、Android和Web上构建美观的原生用户界面。使用单一代码库,开发人员可以创建高性能、流畅的应用程序。ReactNative:......