当前位置:网站首页>【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
}
边栏推荐
- MySQL安装教程
- 奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
- 【pytorch记录】pytorch的分布式 torch.distributed.launch 命令在做什么呢
- 【插值与拟合】
- TypeError The view function did not return a valid response. The function either returned None 的解决
- [MySQL exercises] Chapter 2 Basic operations of databases and data tables
- MySQL 5.7 安装教程(全步骤、保姆级教程)
- 使用MySQL如何查询一年中每月的记录数
- Machine Learning - Notes and Implementation of Linear Regression, Logistic Regression Problems
- 【黄啊码】MySQL入门—3、我用select ,老板直接赶我坐火车回家去,买的还是站票
猜你喜欢
【MySQL中auto_increment有什么作用?】
一、MySQL主从复制原理
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
《如何戒掉坏习惯》读书笔记
MySQL installation to the last step in the write the configuration file failed?And after the installation steps
Ubuntu安装Mysql5.7
Visual Studio新功能出炉:低优先级构建
[Cloud native] Introduction and use of Feign of microservices
vscode输出中文乱码问题
随机推荐
Cloud server deployment web project
torch分布式训练
SSM框架简单介绍
【MySQL功法】第4话 · 和kiko一起探索MySQL中的运算符
Docker-compose安装mysql
[MySQL exercises] Chapter 2 Basic operations of databases and data tables
First acquaintance with NK-RTU980 development board
MySql数据库优化查询工具
Visual Studio新功能出炉:低优先级构建
[Cloud native and 5G] Microservices support 5G core network
【插值与拟合】
Open Source | Commodity Recognition Recommender System
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI
哪些字符串会被FastJson解析为null呢
"C language" frog jumping steps recursion problem
MySQL中InnoDB的多版本并发控制(MVCC)的实现
SQL 入门之第一讲——MySQL 8.0.29安装教程(windows 64位)
WLAN部署(AC+AP)配置及常见问题记录
关于yum源的配置及更新
最大似然估计和最小二乘法 含代码