所有资源导入器设置的默认编辑器。
使用默认编辑器可为资源编辑导入设置。可以为特定资源类型定义一个自定义导入设置编辑器。为此,请创建从 AssetImporterEditor 继承的新类并使用引用 ScriptedImporter 的 CustomEditorAttribute。
以下示例说明如何为具有自定义布局的 ScriptedImporter 创建一个自定义 ScriptedImporterEditor。
using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine;
[CustomEditor(typeof(TransformImporter))] [CanEditMultipleObjects] public class TransformImporterEditor : ScriptedImporterEditor { // Stored SerializedProperty to draw in OnInspectorGUI. SerializedProperty m_GenerateChild;
public override void OnEnable() { base.OnEnable(); // Once in OnEnable, retrieve the serializedObject property and store it. m_GenerateChild = serializedObject.FindProperty("generateChild"); }
public override void OnInspectorGUI() { // Update the serializedObject in case it has been changed outside the Inspector. serializedObject.Update();
// Draw the boolean property. EditorGUILayout.PropertyField(m_GenerateChild);
// Apply the changes so Undo/Redo is working serializedObject.ApplyModifiedProperties();
// Call ApplyRevertGUI to show Apply and Revert buttons. ApplyRevertGUI(); } }
[ScriptedImporter(0, ".transform")] public class TransformImporter : ScriptedImporter { public bool generateChild;
public override void OnImportAsset(AssetImportContext ctx) { GameObject root = ObjectFactory.CreateGameObject(Path.GetFileNameWithoutExtension(ctx.assetPath)); if (generateChild) { GameObject child = ObjectFactory.CreateGameObject("child"); child.transform.SetParent(root.transform); } ctx.AddObjectToAsset("main", root); ctx.SetMainObject(root); } }
以下示例说明用户无法更改设置并使用 needsApplyRevert 隐藏了 Apply/Revert 按钮的特定情况。
using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine;
[CustomEditor(typeof(EmptinessImporter))] [CanEditMultipleObjects] public class EmptinessImporterEditor : ScriptedImporterEditor { //Let the parent class know that the Apply/Revert mechanism is skipped. protected override bool needsApplyRevert => false;
public override void OnInspectorGUI() { // Draw some information EditorGUILayout.HelpBox("Because this Importer doesn't have any settings, the Apply/Revert buttons are hidden.", MessageType.None); } }
[ScriptedImporter(0, ".emptiness")] public class EmptinessImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { GameObject root = ObjectFactory.CreateGameObject(Path.GetFileNameWithoutExtension(ctx.assetPath)); ctx.AddObjectToAsset("main", root); ctx.SetMainObject(root); } }
以下示例说明如何在自定义 AssetImporterEditor 中使用 extraDataType 读取或保存不属于 ScriptedImporter 序列化一部分的设置。
using System; using System.IO; using UnityEditor; using UnityEditor.AssetImporters; using UnityEngine; using Object = UnityEngine.Object;
[CustomEditor(typeof(BooleanImporter))] [CanEditMultipleObjects] public class BooleanImporterEditor : ScriptedImporterEditor { // Property to show in the custom OnInspectorGUI. SerializedProperty m_BooleanProperty;
// override extraDataType to return the type that will be used in the Editor. protected override Type extraDataType => typeof(BooleanClass);
// override InitializeExtraDataInstance to set up the data. protected override void InitializeExtraDataInstance(Object extraTarget, int targetIndex) { var boolean = (BooleanClass)extraTarget; // Read the boolean value from the text file and fill the extraTarget object with the data. string fileContent = File.ReadAllText(((AssetImporter)targets[targetIndex]).assetPath); if (!bool.TryParse(fileContent, out boolean.boolean)) { boolean.boolean = false; } }
protected override void Apply() { base.Apply(); // After the Importer is applied, rewrite the file with the custom value. for (int i = 0; i < targets.Length; i++) { string path = ((AssetImporter)targets[i]).assetPath; File.WriteAllText(path, ((BooleanClass)extraDataTargets[i]).boolean.ToString()); } }
public override void OnEnable() { base.OnEnable(); // In OnEnable, retrieve the importerUserSerializedObject property and store it. m_BooleanProperty = extraDataSerializedObject.FindProperty("boolean"); }
public override void OnInspectorGUI() { // Note: you don't need to call serializedObject.Update or serializedObject.ApplyModifiedProperties // because you are not changing the target (serializedObject) itself.
// Update the importerUserSerializedObject in case it has been changed outside the Inspector. extraDataSerializedObject.Update();
// Draw the boolean property. EditorGUILayout.PropertyField(m_BooleanProperty);
// Apply the changes so Undo/Redo is working. extraDataSerializedObject.ApplyModifiedProperties();
// Call ApplyRevertGUI to show Apply and Revert buttons. ApplyRevertGUI(); } }
public class BooleanClass : ScriptableObject { public bool boolean; }
[ScriptedImporter(0, ".boolean")] public class BooleanImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { string fileContent = File.ReadAllText(ctx.assetPath); var booleanObj = ObjectFactory.CreateInstance<BooleanClass>(); if (!bool.TryParse(fileContent, out booleanObj.boolean)) { booleanObj.boolean = false; } ctx.AddObjectToAsset("main", booleanObj); ctx.SetMainObject(booleanObj); Debug.Log("Imported Boolean value: " + booleanObj.boolean); } }
还可以在相同 AssetImporterEditor 中使用 ScriptedImporter 设置和 extraData:
using UnityEditor; using UnityEditor.AssetImporters;
[CustomEditor(typeof(SomeScriptedImporter))] [CanEditMultipleObjects] public class SomeImporterEditor : ScriptedImporterEditor { // ...
public override void OnInspectorGUI() { serializedObject.Update(); extraDataSerializedObject.Update();
// Use propertyDrawers and custom GUI for any property from both serializedObject and extraDataSerializedObject.
extraDataSerializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
ApplyRevertGUI(); } }
[ScriptedImporter(0, ".someFile")] public class SomeScriptedImporter : ScriptedImporter { public override void OnImportAsset(AssetImportContext ctx) { // ... } }
extraDataSerializedObject | 一个 SerializedObject,表示 AssetImporterEditor 的 extraDataTarget 或 extraDataTargets。 |
extraDataTarget | 与 Editor.target 关联的额外数据对象。 |
extraDataTargets | 与每个 Editor.targets 关联的对象数组。 |
extraDataType | 覆盖此属性以返回从 ScriptableObject 继承的类型。这使 AssetImporterEditor 可了解导入器外部的序列化数据。 |
needsApplyRevert | 是否需要在检视面板中绘制 ApplyRevertGUI 方法。 |
showImportedObject | 导入的对象是否应显示为独立编辑器。 |
useAssetDrawPreview | 确定是由 AssetEditor 还是 Importer DrawPreview 处理资源预览。 |
HasModified | 确定导入设置是否已修改。 |
OnDisable | 当编辑器对象超出范围时调用此函数。 |
OnEnable | 当对象加载时调用此函数。 |
OnInspectorGUI | 重写此方法,以针对 ScriptedImporter 创建自己的检视面板 GUI。 |
Apply | 将编辑器控件的所有更改保存到资源的导入设置对象中。 |
ApplyButton | 实现 Inspector 的“Apply”按钮。 |
ApplyRevertGUI | 向编辑器添加“Apply”和“Revert”按钮。 |
Awake | 当编辑器脚本启动时调用此函数。 |
CanApply | Determines if the modifications to import settings can be applied. |
InitializeExtraDataInstance | 在编辑器的初始化过程中调用此方法(在 Awake 之后,OnEnable 之前)。 |
OnApplyRevertGUI | 处理“Apply”和“Revert”按钮。 |
RevertButton | 实现 Inspector 的“Revert”按钮。 |
hasUnsavedChanges | This property specifies whether the Editor prompts the user to save or discard unsaved changes before the Inspector gets rebuilt. |
saveChangesMessage | The message that displays to the user if they are prompted to save. |
serializedObject | 表示正在检查的一个或多个对象的 SerializedObject。 |
target | 所检查的对象。 |
targets | 正在检查的所有对象的数组。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
CreateInspectorGUI | 实现此方法以创建自定义 UIElements 检视面板。 |
CreatePreview | Implement this method to make a custom UIElements inspector preview. |
DiscardChanges | Discards unsaved changes to the contents of the editor. |
DrawDefaultInspector | Draws the built-in Inspector. |
DrawHeader | 调用此函数以绘制编辑器标头。 |
DrawPreview | 预览绘制的第一个入口点。 |
GetInfoString | 实现此方法以在资源预览上显示资源信息。 |
GetPreviewTitle | 如果要更改 Preview 区域的标签,可重载此方法。 |
HasPreviewGUI | 如果实现 OnPreviewGUI,可在子类中重载此方法。 |
OnInteractivePreviewGUI | 实现此方法可创建您自己的交互式自定义预览。交互式自定义预览用于检视面板和对象选择器的预览区域。 |
OnPreviewGUI | Creates a custom preview for the preview area of the Inspector, the headers of the primary Editor, and the object selector.You must implement Editor.HasPreviewGUI for this method to be called. |
OnPreviewSettings | 如果要在预览标头中显示自定义控件,可重载此方法。 |
RenderStaticPreview | 如果要渲染静态预览,可重载此方法。 |
Repaint | 重新绘制显示此编辑器的任意检视面板。 |
RequiresConstantRepaint | 检查此编辑器在其当前状态下是否需要不断重绘。 |
SaveChanges | Performs a save action on the contents of the editor. |
UseDefaultMargins | 如果您不想使用默认边距,可在子类中重载此方法以返回 false。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
ShouldHideOpenButton | 返回检视面板中“open”按钮的可见性设置。 |
CreateCachedEditor | 返回时,previousEditor 是 targetObject 或 targetObjects 的编辑器。如果该编辑器已经在跟踪对象,则函数会返回,否则该函数会销毁之前的编辑器并创建一个新编辑器。 |
CreateCachedEditorWithContext | 使用上下文对象创建缓存的编辑器。 |
CreateEditor | 为 targetObject 或 targetObjects 创建自定义编辑器。 |
CreateEditorWithContext | 使用上下文对象为 targetObject 或 targetObjects 创建自定义编辑器。 |
DrawFoldoutInspector | 为 target 绘制带有折叠标头的检视面板 GUI。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
CreateInstance | 创建脚本化对象的实例。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |
HasFrameBounds | Validates whether custom bounds can be calculated for this Editor. |
OnGetFrameBounds | Gets custom bounds for the target of this editor. |
OnSceneGUI | 使编辑器在场景视图中处理事件。 |
OnDestroy | 当脚本化对象将销毁时调用此函数。 |
OnValidate | Editor-only function that Unity calls when the script is loaded or a value changes in the Inspector. |
Reset | 重置为默认值。 |
finishedDefaultHeaderGUI | 在绘制默认标题项之后,在绘制 Inspector 窗口的标题时引发的事件。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.