当前位置:网站首页>【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
}
边栏推荐
猜你喜欢

Open Source | Commodity Recognition Recommender System
![[MySQL exercises] Chapter 2 Basic operations of databases and data tables](/img/43/73a59a293d4708b6f9aeae990a7029.png)
[MySQL exercises] Chapter 2 Basic operations of databases and data tables

一文搞定代码中的命名

Locust 1.0版本引入的变化

Install the deployment kubernetes KubeSphere management

Flutter Paystack 所有选项实现

Practical Bioinformatics 2: Multi-omics data integration and mining

New in Visual Studio: Low Priority Builds

一、MySQL主从复制原理

【云原生与5G】微服务加持5G核心网
随机推荐
[Cloud native and 5G] Microservices support 5G core network
MySQL 5.7 安装教程(全步骤、保姆级教程)
CNN--Introduction to each layer
分布式缓存系统必须要解决的四大问题
PowerCLi 通过自建PXE Server一键esxi7下批量部署常规New-VM
Ubuntu安装Mysql5.7
How to upgrade nodejs version
Install the deployment kubernetes KubeSphere management
【小程序项目开发-- 京东商城】uni-app之自定义搜索组件(下) -- 搜索历史
[PSQL] Complex query
实用生物信息学2:多组学数据整合和挖掘
sqlmap使用教程大全命令大全(图文)
mysql insert new field method
"C language" frog jumping steps recursion problem
开源|商品识别推荐系统
云服务器部署 Web 项目
2022杭电杯超级联赛3
【MySQL中auto_increment有什么作用?】
关于yum源的配置及更新
Regarding "computing power", this article is worth reading