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. 『Plotly实战指南』--Plotly与Pandas的深度融合

    在数据分析的世界中,数据处理与可视化是密不可分的两个环节. Pandas作为Python数据处理的核心工具,以其强大的数据清洗.转换和分析能力,成为数据科学家和分析师的必备利器: 而Plotly则是交 ...

  2. Python标准库学习之logging

    前言 在python程序中,出于调试监测或者追踪(track)的目的,一般会在关键的部位加print语句来输出监测的变量值,但对于复杂的程序来讲,这样的调试手段就远远不够了,这也是logging库存在 ...

  3. 一个基于 ASP.NET Core 实现的领域驱动设计落地战术框架

    前言 今天大姚给大家分享一个基于 ASP.NET Core 实现的领域驱动设计落地战术框架:netcorepal-cloud-framework. 框架介绍 netcorepal-cloud-fram ...

  4. Markdown中设置图片尺寸及添加图注

    设置缩放比例 使用下面的语法可以调整图片尺寸,同时保证长宽比: <img style="width:缩放比例;" src="图片资源地址"/> 在标 ...

  5. PINN做脆性材料裂纹扩展的损失函数设计

  6. Django的Model类

    1.model 用来和数据交互的(读取和传入数据) 2.orm Object Relational Mapping对象关系映射,python中的model对象和数据库中的表做映射 3.重要概念 3.1 ...

  7. ResizeObserver和IntersectionObserver的详细讲解

    ResizeObserver 的介绍 ResizeObserver 用于异步观察元素的尺寸变化. 如:SVG 元素或文本节点的大小变化.调整浏览器窗口大小.动态改变某个元素的大小时 可以触发相应的回调 ...

  8. php数据结构中的链表

    本文由 ChatMoney团队出品 链表的基本概念 链表(Linked List)是一种常见的数据结构,它由一系列节点组成,每个节点除了存储数据外,还包含指向下一个节点的指针.与数组相比,链表在插入和 ...

  9. javacv添加字幕 剧中显示

    介绍 javacv目前不能像ffmpeg那样 直接加载字体文件到视频 参考这里 所以实现流程为:提取帧 -> 转图片 -> 编辑图片增加文字 -> 转回帧 -> 输出视频 上代 ...

  10. 函数使用十三:BAPI_REQUISITION_CREATE

    *&---------------------------------------------------------------------**& Report  ZBAPI_REQ ...