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
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
}