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

Unity 编辑器格子工具的更多相关文章

  1. Unity编辑器 - 资源批处理工具基类

    Unity编辑器 - 资源批处理工具基类 经常要对资源进行批处理,很多时候都是一次性的需求,于是弄个通用脚本. 工具是个弹出面板,处理过程有进度条,如下: 如图,子类只需要重写几个方法: using ...

  2. 【Unity】2.1 初识Unity编辑器

    分类:Unity.C#.VS2015 创建日期:2016-03-26 一.简介 本节要点:了解Unity编辑器的菜单和视图界面,以及最基本的操作,这是入门的最基础部分,必须掌握. 二.启动界面 双击桌 ...

  3. Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据

    Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject  Unity编辑器扩展 Chapt ...

  4. Unity 编辑器扩展 Chapter2—Gizmos

    二. 使用Gizoms绘制网格及矩阵转换使用 1. 创建Leve类,作为场景控制类: using UnityEngine; //使用namespace方便脚本管理 namespace RunAndJu ...

  5. Unity编辑器扩展chapter1

    Unity编辑器扩展chapter1 unity通过提供EditorScript API 的方式为我们提供了方便强大的编辑器扩展途径.学好这一部分可以使我们学会编写一些工具来提高效率,甚至可以自制一些 ...

  6. Unity编辑器 - 编辑器控制特效播放

    编辑器控制特效播放 Unity的动画编辑器不能预览粒子系统的播放,为了方便预览特效,设想制作一个预览特效的工具,通常一个特效有三种组件: - Animation - Animator - Partic ...

  7. UNITY编辑器模式下static变量的坑

    在unity中写编辑器扩展工具,如在编辑器中加个菜单,点击这个菜单项时执行打包功能. 类如下,其中的静态变量,如果每次进来不清空,则LIST会越来越大,打包函数执行完后系统不会帮我们清空 #if UN ...

  8. unity 编辑器扩展简单入门

    unity 编辑器扩展简单入门 通过使用编辑器扩展,我们可以对一些机械的操作实现自动化,而不用使用额外的环境,将工具与开发环境融为一体:并且,编辑器扩展也提供GUI库,来实现可视化操作:编辑器扩展甚至 ...

  9. Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】

    Unity学习参考文档和开发工具 unity的官网文档:https://docs.unity3d.com/cn/current/Manual/ScriptingSection.html ■ 学习方式: ...

  10. 定制你的Unity编辑器

    Unity的编辑器可以通过写脚本进行界面定制,包括添加功能菜单,今天写游戏Demo用到了记录一下. 为Unity添加子菜单 示例程序 [AddComponentMenu("Defend Ho ...

随机推荐

  1. CSS 魔法与布局技巧

    CSS 布局与视觉效果常用实践指南 在我一篇随笔中其实有说到十大布局,里面有提到 flex 布局.grid 布局.响应式布局,不过没有提到容器查询这个,现在说下这三个布局然后穿插下容器查询把. 1️⃣ ...

  2. Axure RP仿抖音短视频APP交互原型图模板

    Axure RP仿抖音短视频APP高保真交互原型模板,原型图设计灵感来自于抖音段视频APP,在预览里你可以看到抖音的影子.本素材包含登录.首页推荐.同城.直播间.消息.朋友.发布.我的.搜索等主要模块 ...

  3. MySql的information_schema.processlist库学习之"如何检测出大数据sql查询"

    1.如何通过MySql检测出大数据sql查询 一般数据库都会存在:information_schema数据库 检测出大数据sql查询[time时间越长说明,数据量越大,要根据公司的限度来衡量,我的思路 ...

  4. 鸿蒙Next元服务开发详解

    之前写过关于元服务的文章,大家对元服务应该也有一定的了解,它是一种更加高效便捷的应用形式,免安装,有独立的入口,说的简单一点就像是把微信小程序放到系统层面,相比微信小程序更加快捷,因为连微信也不用打开 ...

  5. MySQL5.7小白安装教程(提供安装包)

    下载地址:https://dev.mysql.com/downloads/installer/ (如果网络不稳定下不下来,别急,我已经为你们下好了.到我公众号[勾玉技术]回复[mysql5.7]获取百 ...

  6. Claude 4炸裂发布!凭什么敢称宇宙最强编程 AI?

    5 月 23 日,Claude 官方宣布推出下一代 Claude 模型:Claude Sonnet 4 和 Claude Opus 4,直接 在全世界的 AI 圈掀起了新的风暴! 据官方称,Claud ...

  7. 【深入理解Base64编码】原理、应用与Java实战

    深入理解Base64编码:原理.应用与Java实战 目录 1-什么是base64编码 2-base64编码原理详解 3-base64的java实现 4-高级应用与技巧 5-注意事项与常见误区 6-总结 ...

  8. Windows基线检查

    Windows 脆弱性检查是指通过一系列工具和方法,评估 Windows 操作系统中的潜在安全漏洞,以发现并修复可能被攻击者利用的弱点.这个过程包括对系统的安全配置.补丁更新.权限设置以及其他安全因素 ...

  9. Centos7.x根分区扩容

    背景说明 我们在部署好的系统中,随着数据的不断增加, 发现根分区频繁出现满载问题,这种情况下,我们需要对根分区进行扩容. 方案说明 • 使用空闲磁盘扩容到根分区 • 使用空闲的分区扩容到根分区 • 使 ...

  10. 能提高你的创作效率的超强AI工具:ChatMoney

    本文由 ChatMoney团队出品 引言 在广告创意行业,创新和高效是赢得市场的关键.而我今天要分享的就是如何利用ChatMoney这款强大的人工智能工具,打破创新难题,赚取丰厚收益. 让我告诉你一个 ...