当前位置:网站首页>【Unity】编辑器扩展-03-拓展Inspector视图
【Unity】编辑器扩展-03-拓展Inspector视图
2022-07-31 07:52:00 【韩天衣】
【Unity】编辑器扩展-03-拓展Inspector视图
拓展原生组件
CustomEditor()表示自定义哪个组件,OnInspectorGUI()可以对该组件元素进行绘制,base.OnInspectorGUI()表示绘制父类原有元素
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Camera))]
public class Script_08 : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("click"))
{
Debug.Log("click");
}
}
}
拓展继承组件
如果按照之前的方式直接拓展,则会导致Inspector界面绘制发生变化,但我们希望拓展面板后仍然保持原有绘制方式。
Unity将大量的Editor绘制方法封装在内部的DLL文件中,开发者无法调用。
因此我们需要使用C#反射的方式调用内部未公开的方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
[CustomEditor(typeof(Transform))]
public class Script_09 : Editor
{
private Editor m_Editor;
void OnEnable()
{
m_Editor = Editor.CreateEditor(target,Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.TransformInspector",true));
}
public override void OnInspectorGUI()
{
m_Editor.OnInspectorGUI();
if (GUILayout.Button("click"))
{
}
}
}
组件不可编辑

[CustomEditor(typeof(Transform))]
public class Script_09 : Editor
{
private Editor m_Editor;
void OnEnable()
{
m_Editor = Editor.CreateEditor(target,Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.TransformInspector",true));
}
public override void OnInspectorGUI()
{
GUI.enabled = false;
m_Editor.OnInspectorGUI();
GUI.enabled = true;
if (GUILayout.Button("click"))
{
}
}
}
自定义锁功能菜单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_10 : MonoBehaviour
{
[MenuItem("GameObject/3D Object/Lock/Lock",false,0)]
static void Lock()
{
if (Selection.gameObjects != null)
{
foreach (var gameObject in Selection.gameObjects)
{
gameObject.hideFlags = HideFlags.NotEditable;
}
}
}
[MenuItem("GameObject/3D Object/Lock/UnLock", false, 0)]
static void UnLock()
{
if (Selection.gameObjects != null)
{
foreach (var gameObject in Selection.gameObjects)
{
gameObject.hideFlags = HideFlags.None;
}
}
}
}
HideFlags(它可以使用位运算)
HideFlags.None;清楚状态HideFlags.DontSave;设置对象不会被保存(仅编辑模式下)HideFlags.DontSaveInBuild;设置对象构建后不会被保存HideFlags.DontSaveInEditor;设置对象编辑模式下不会被保存HideFlags.DontUnloadUnusedAsset;设置对象不会被Resources.UnloadUnusedAssets()卸载无用资源时卸掉HideFlags.HideAndDontSave;设置对象隐藏,并且不会被保存HideFlags.HideInHierarchy;设置对象在层次视图中隐藏HideFlags.HideInInspector;设置对象在控制面板视图中隐藏HideFlags.NotEditable;设置对象不可被编辑
Context菜单
前缀菜单
CONTEXT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_11
{
[MenuItem("CONTEXT/Transform/new Context 1")]
public static void NewContext1(MenuCommand menuCommand)
{
Debug.Log(menuCommand.context.name);
}
}
我们还可可以通过context来访问脚本本身。
此外,通过宏定义标签UNITY_EDITOR表示这段代码只会在Editor模式下执行,发布后将会被剔除
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Script_11 : MonoBehaviour
{
string number;
#if UNITY_EDITOR
[MenuItem("CONTEXT/Script_11/new Context 1")]
public static void NewContext1(MenuCommand menuCommand)
{
//访问脚本变量
Script_11 script_11 = menuCommand.context as Script_11;
script_11.number = "10";
Debug.Log(menuCommand.context.name);
}
#endif
}
边栏推荐
- 《c语言小游戏》入门级三子棋游戏(机器人加强版)
- [Cloud native] Introduction and use of Feign of microservices
- Collation and sharing of related classic papers and datasets in the field of deep learning communication
- 关于@Autowired
- Shell编程之条件语句
- The Spark run on Yarn Spark application
- 日志导致线程Block的这些坑,你不得不防
- New in Visual Studio: Low Priority Builds
- XSS靶场prompt.ml过关详解
- MySql数据库优化查询工具
猜你喜欢

LED flashing on CY7C68013A

PHP中 比较 0、false、null,‘‘ “

Open Source | Commodity Recognition Recommender System

R语言 第一部分

【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史

Practical Bioinformatics 2: Multi-omics data integration and mining

NK - RTU980 burning bare-metal program

日志导致线程Block的这些坑,你不得不防

Vulkan与OpenGL对比——Vulkan的全新渲染架构

【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
随机推荐
《c语言小游戏》入门级三子棋游戏(机器人加强版)
7/28-7/29 Expectation + thinking + suffix array + ST table
The first part of the R language
关于“算力”,这篇文章值得一看
循环结构--for循环
MySQL安装常见报错处理大全
Shell编程之条件语句
PowerCLi 通过自建PXE Server一键esxi7下批量部署常规New-VM
XSS靶场prompt.ml过关详解
uniapp 高度不自适应
"C language game" entry-level chess game (robot enhanced version)
如何升级nodejs版本
安装部署KubeSphere管理kubernetes
[Cloud native] Introduction and use of Feign of microservices
一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
linux redis6.2.6配置文件
C语言三子棋(井字棋)小游戏
LED flashing on CY7C68013A
关于Error EPERM operation not permitted, mkdir...几种解决办法的比较
Ubuntu22.04安装mysql