当前位置: 首页 > news >正文

Unity Editor实用功能:Hierarchy面板的对象上绘制按按钮并响应

目录

  • 需求描述
  • 上代码
  • 打个赏吧

需求描述

现在有这样一个需求:

  • 在Hierarchy面板的对象上绘制按钮
  • 点击按钮,弹出菜单
  • 再点击菜单项目响应自定义操作
  • 在这里的响应主要是复制对象层级路路径
    看具体效果请看动图:
    请添加图片描述

注:

  • 核心是对EditorApplication.hierarchyWindowItemOnGUI委托的实现
  • 其它需求,可参考实现
  • 如是要要Project面板实现类似的功能:可以参考实现EditorApplication.hierarchyWindowItemOnGUI委托

上代码

/*********************************************** @author: anyuanlzh* @date: 2023-05-18* @des:  "Hierarchy面板"工具***********************************************/using System.Collections.Generic;
using UnityEditor;
using UnityEngine;[InitializeOnLoad]
public class HierarchyTabTool
{// 静态构造函数static HierarchyTabTool(){EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;}private static void HierarchyWindowItemOnGUI(int instanceId, Rect selectionRect){var obj = EditorUtility.InstanceIDToObject(instanceId) as GameObject;if (obj == null)return;GameObject selectedObjs = Selection.activeGameObject;if(obj!=selectedObjs)return;selectionRect.x += selectionRect.width - 60;selectionRect.y += 0;selectionRect.width = 60f;GUIStyle fontStyle = new GUIStyle(GUI.skin.button);fontStyle.alignment=TextAnchor.MiddleCenter;fontStyle.fontSize=10;fontStyle.normal.textColor=Color.yellow;//点击事件if (GUI.Button(selectionRect, "复制层级", fontStyle)){// Debug.Log($"click: {Selection.activeObject.name}");// 弹出菜单Vector2 mousePosition = Event.current.mousePosition;Rect position = new Rect(mousePosition.x, mousePosition.y+7, 0, 0);EditorUtility.DisplayPopupMenu(position, "GameObject/1_复制层级路径", null);}}// 防止一次点击响应多次private static float _last_call_time = 0;private static float minInterval_time = 0.5f;[MenuItem("GameObject/1_复制层级路径/A点~B点", false, 40)]private static void GetHierarchyPath2(){if (Time.time - _last_call_time<minInterval_time){return;}_last_call_time = Time.time;GameObject[] selectedObjs = Selection.gameObjects;//Debug.Log(selectedObjs.Length);if (selectedObjs.Length == 1){Copy_HierarchyPath_root2target();return;}else if (selectedObjs.Length < 2){Debug.Log("请选择一个或二个有包含关系对象");return;}Transform first = selectedObjs[0].transform;Transform last = selectedObjs[selectedObjs.Length-1].transform;// Debug.Log($"first.name:{first.name} last.name:{last.name}");Transform a = null;Transform b = null;if (EditorUtils.IsAncestor(first, last)){a = first;b = last;}else if (EditorUtils.IsAncestor(last, first)){a = last;b = first;}else{Debug.LogError("请选择有包含关系的二个对象");return;}List<string> names = new List<string>();while (b!=null){if (a == b){names.Insert(0,b.name);break;}names.Insert(0, b.name);b = b.parent;}string path = "";for (int i = 0; i < names.Count-1; i++){path += names[i] + "/";}path += names[^1];GUIUtility.systemCopyBuffer = path;Debug.Log("对象层次路径 A点到B点: " + path);}[MenuItem("GameObject/1_复制层级路径/根0~目标", false, 40)]private static void Copy_HierarchyPath_root0target(){Copy_HierarchyPath_rootN2target(0);}[MenuItem("GameObject/1_复制层级路径/根1~目标", false, 40)]private static void Copy_HierarchyPath_root1target(){Copy_HierarchyPath_rootN2target(1);}[MenuItem("GameObject/1_复制层级路径/根2~目标", false, 40)]private static void Copy_HierarchyPath_root2target(){Copy_HierarchyPath_rootN2target(2);}[MenuItem("GameObject/1_复制层级路径/根3~目标", false, 40)]private static void Copy_HierarchyPath_root3target(){Copy_HierarchyPath_rootN2target(3);}// 从根0的第N级到目标// rootN从零开static void Copy_HierarchyPath_rootN2target(int rootN){if (Time.time - _last_call_time<minInterval_time){return;}_last_call_time = Time.time;if (Selection.count != 1){Debug.LogError($"Copy_HierarchyPath_rootN2target: 请选择一个对象");return;}Transform target = Selection.activeGameObject.transform;List<string> names = new List<string>();Transform parent = target.transform.parent;while (target != null){names.Insert(0, target.name);target = target.parent;}if (names.Count - 1 < rootN){Debug.LogError($"Copy_HierarchyPath_rootN2target: N:{rootN}大于目标对象的深度");return;}string path = "";for (int i = rootN; i < names.Count-1; i++){path += names[i] + "/";}path += names[^1];GUIUtility.systemCopyBuffer = path;Debug.Log($"对象层次路径 root_{rootN}到target:" + path);}
}

打个赏吧

请添加图片描述

http://www.lryc.cn/news/277228.html

相关文章:

  • 解决录制的 mp4 视频文件在 windows 无法播放的问题
  • 一键与图片对话!LLM实现图片关键信息提取与交互
  • 洛谷 P8833 [传智杯 #3 决赛] 课程 讲解
  • 中国IT产经新闻:新能源汽车发展前景与燃油车的利弊之争
  • 一、数据结构
  • 案例分享:各行业销售岗位的KPI指标制定分享
  • 【办公类-19-01】20240108图书统计登记表制作(23个班级)EXCEL复制表格并合并表格
  • spring boot 2升级为spring boot 3中数据库连接池druid的问题
  • 客服系统配置之Nginx处理静态资源和动态请求
  • Golang 切片
  • 防止公司办公终端文件数据 | 资料外泄,——自动智能透明加密防泄密软件系统
  • C#-枚举
  • Java后端开发——SSM整合实验
  • VMware虚拟机安装Ubuntu
  • 用一个简单的例子说明单细胞分析中的dgCMatrix数据的结构
  • 【小工具】pixi-live2d-display,直接可用的live2d的交互网页/桌面应用
  • vulhub中的Nginx漏洞的详细解析
  • 如何实现公网访问GeoServe Web管理界面共享空间地理信息【内网穿透】
  • k8s-存储 11
  • 蓝牙信标定位原理
  • 单片机期末复习
  • springboot虹软人脸识别集成
  • Element+vue3.0 tabel合并单元格span-method
  • Python学习笔记第七十九天(OpenCV轨迹栏)
  • uniapp自定义顶部导航并解决打包成apk后getMenuButtonBoundingClientRect方法失效问题
  • C++入门【26-C++ Null 指针】
  • Linux第14步_安装FTP服务器
  • Linux截图方法推荐
  • 在Gitee上维护Erpnext源
  • 2024.1.9 基于 Jedis 通过 Java 客户端连接 Redis 服务器