当前位置:网站首页>【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#】判断字符串中是否包含指定字符或字符串(Contains/IndexOf)
CNN--Introduction to each layer
Client navicat installation tutorial
深度理解递归,手撕经典递归问题(汉诺塔,青蛙跳台阶),保姆级教学。
How on one machine (Windows) to install two MYSQL database
Ceph single node deployment
【面试题】从输入URL到游览器渲染完成,经历了什么
如何在 Linux 上安装 MySQL
7/28-7/29 期望+思维+后缀数组+ST表
【云原生与5G】微服务加持5G核心网
MySQL安装教程
Reimbursement Process | By Tianfang
【MySQL功法】第3话 · MySQL中常见的数据类型
Ubuntu22.04安装mysql
Calculation example of matlab program iEEE9 node system for power flow calculation of AC-DC hybrid system based on alternate iteration method
深度学习随机数设置,保证实验的可重复性
torch分布式训练
[What is the role of auto_increment in MySQL?】
关于Error EPERM operation not permitted, mkdir...几种解决办法的比较
蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型