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

unity编辑器扩展dll形式展示

1.背景:最近搞工程迁移发现一旦c#报错就会导致编辑器菜单没法使用,做了一些尝试发现使用dll的方式会是不错的选择。当然有些工具还是建议用外部的c#工程来写比如winform.

2.遇到的问题:我记得之前2017年左右的时候做一个unity的dll工程并不需要引入多少unity的内置dll一般引入UnityEngine.UI.dll 以及UnityEngine.dll就可以了

看看现在的编辑器使用的dll(你的目录\Unity\Hub\Editor\2021.3.41f1\Editor\Data\Managed\UnityEngine\一堆dll)

实例:SceneViewUI.cs 这个例子会比较直观 生成dll 放在unity的Assets/Plugin/Editor(自行构建)/xx.dll  以及xx.pdb(支持调试功能)

using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;[InitializeOnLoad]
public class SceneViewUI
{static SceneViewUI(){// 订阅 SceneView.duringSceneGui 事件SceneView.duringSceneGui += OnSceneGUI;}private static void OnSceneGUI(SceneView sceneView){//if(sceneView.camera.orthographic) // 如果是2d模式直接return //{//    return;//}// 在 Scene 视图中绘制文本Handles.BeginGUI();var oldColor = GUI.color;GUI.color = Color.black;Rect sceneViewRect = SceneView.currentDrawingSceneView.position;// 计算 Box 在右下角的位置float boxX = sceneViewRect.width - 300;float boxY = sceneViewRect.height - 50;// 创建一个 GUILayout 区域,将其放置在右下角GUILayout.BeginArea(new Rect(boxX, boxY, 300, 100));// 创建一个 GUILayout.BoxGUILayout.Box("按下F12直接跳转到选中gameObject对应的代码行");GUILayout.EndArea();Vector3 bottomLeftWorld = sceneView.camera.ViewportToWorldPoint(new Vector3(0, 0, sceneView.camera.nearClipPlane));Vector2 bottomLeftScreen = HandleUtility.WorldToGUIPoint(bottomLeftWorld);var rect = new Rect(bottomLeftScreen.x, bottomLeftScreen.y - 20, 200, 20);var size = GameViewTools.GameViewSize();string isLand = (size.x > size.y) ? "横屏" : "竖屏";GUI.Label(rect, $"屏幕({isLand})分辨率为:({size.x},{size.y})");GUI.color = oldColor;rect = new Rect(rect.x, rect.y - 20, 200, 20);if (GUI.Button(rect, "启动游戏")){EasyUseEditorTool.OnSceneOpenOrPlay("Assets/scenes/GameStart.unity");}rect = new Rect(rect.x, rect.y - 20, 200, 20);if ((!EditorPrefs.GetBool("InitializerUiEdit", false) && GUI.Button(rect, "锁定分辨率"))|| (EditorPrefs.GetBool("InitializerUiEdit", false) && GUI.Button(rect, "解除锁定分辨率"))){EditorPrefs.SetBool("InitializerUiEdit", !EditorPrefs.GetBool("InitializerUiEdit", false));}rect = new Rect(rect.x, rect.y - 20, 200, 20);if ((!EditorPrefs.GetBool("SelectionTools", true) && GUI.Button(rect, "资源自动展开"))|| (EditorPrefs.GetBool("SelectionTools", true) && GUI.Button(rect, "解除资源自动展开"))){EditorPrefs.SetBool("SelectionTools", !EditorPrefs.GetBool("SelectionTools", false));AssetDatabase.Refresh();}rect = new Rect(rect.x, rect.y - 20, 200, 20);if ((!EditorPrefs.GetBool("customlog", false) && GUI.Button(rect, "开启自定义日志"))|| (EditorPrefs.GetBool("customlog", false) && GUI.Button(rect, "关闭自定义日志"))){EditorPrefs.SetBool("customlog", !EditorPrefs.GetBool("customlog", false));}rect = new Rect(rect.x, rect.y - 20, 200, 20);if (GUI.Button(rect, "打包")){var classType = Type.GetType("PackHelper,Assembly-CSharp-Editor");var binds = BindingFlags.Static | BindingFlags.Instance |BindingFlags.Public | BindingFlags.NonPublic;var methodInfo = classType.GetMethod("BundleSetting",binds);methodInfo.Invoke(null,null);}rect = new Rect(rect.x, rect.y - 20, 200, 20);if (GUI.Button(rect, "资源冗余检查&清理资源")){Resolution rs = Screen.currentResolution; //获取当前的分辨率 int nWidth = 600;int nHeight = 500;int x = (rs.width - nWidth) / 2;int y = (rs.height - nHeight) / 2;Rect rect2 = new Rect(x, y, nWidth, nHeight);FindRepeatRes myWindow = (FindRepeatRes)EditorWindow.GetWindowWithRect(typeof(FindRepeatRes), rect2, true,"资源查重&合并");myWindow.position = rect2;myWindow.Show();//展示 myWindow.closeAction += EditorLogWindow.CloseWindow;EditorCoroutine.StartCoroutine(new EditorWaitForSeconds(0.01f, () =>{EditorLogWindow.OpenWindow(myWindow);myWindow.Focus();}));}rect = new Rect(rect.x, rect.y - 20, 200, 20);if (GUI.Button(rect, "代码同步")){CodeMoveTool.ShowWindow();}rect = new Rect(rect.x, rect.y - 20, 200, 20);if (GUI.Button(rect, "清理缓存")){EditorPrefs.DeleteAll();PlayerPrefs.DeleteAll();string path = Application.persistentDataPath + "/GamebalootRankMatchResult.txt";if (File.Exists(path)){Debug.Log("清理GamebalootRankMatchResult成功");File.Delete(path);}}Handles.EndGUI();}
}

编辑器拓展界面截图:

dll生成 :

右键工程属性 点击生成->事件->生成后事件 配置如下:

xcopy "$(TargetDir)$(TargetName).dll" "D:\code_move1\Assets\Plugins\Editor\" /Y
xcopy "$(TargetDir)$(TargetName).pdb" "D:\code_move1\Assets\Plugins\Editor\" /Y

dll放置位置截图:

http://www.xdnf.cn/news/9723.html

相关文章:

  • 在supermap idesktop中两块影像设置背景透明后,拼接处会有一条明显的黑线,但是放大后又没有,这个怎么处理
  • Java 对接 Office 365 邮箱全攻略:OAuth2 认证 + JDK8 兼容 + Spring Boot 集成(2025 版)
  • 局域网内文件共享的实用软件推荐
  • 利用海外代理IP,做Twitter2026年全球趋势数据分析
  • Spring AI MCP的几个小问题
  • jar包同目录生成文件
  • Linux环境下多进程Socket通信实现
  • 前端面试核心考点全解析
  • Learning Discriminative Data Fitting Functions for Blind Image Deblurring论文阅读
  • C语言编程--20.合并K个升序列表
  • MFC坦克大战游戏制作
  • 禾纳ATE3156AP/ATE3156AS,P沟道增强型MOSFET,替代AO4805方案
  • 数据可视化的定义和类型
  • 基于多尺度卷积和扩张卷积-LSTM的多变量时间序列预测
  • 1、Pytorch介绍与安装
  • C++ RB_Tree
  • 数据仓库分层 4 层模型是什么?
  • 请问Java方法可以同时即是static又是synchronized的吗?
  • 国内高频混压PCB厂家有哪些?
  • react-color-palette源码解析
  • Unity 快捷键
  • Python 字符串入门指南
  • 循环神经网络(RNN)模型
  • vue3 + WebSocket + Node 搭建前后端分离项目 开箱即用
  • 执行npm命令报在此系统禁止运行脚本
  • 多模态大语言模型arxiv论文略读(九十九)
  • 【深度学习】11. Transformer解析: Self-Attention、ELMo、Bert、GPT
  • 【深度学习:进阶篇】--2.3.深度学习正则化
  • 通过远程桌面连接Windows实例提示“出现身份验证错误,无法连接到本地安全机构”错误怎么办?
  • 【Python零基础入门系列】第6篇:Python 的数据结构世界(列表、字典、集合、元组)