所有资源导入器设置的默认编辑器。
使用默认编辑器编辑资源的导入设置。您可以为特定资源类型定义自定义导入设置编辑器。为此,创建一个继承自 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 隐藏了“应用”/“恢复”按钮。
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 | 表示 AssetImporterEditor 的 extraDataTarget 或 extraDataTargets 的 SerializedObject。 |
extraDataTarget | 与 Editor.target 关联的额外数据对象。 |
extraDataTargets | 与每个 Editor.targets 关联的对象数组。 |
extraDataType | 覆盖此属性以返回继承自 ScriptableObject 的类型。这使 AssetImporterEditor 能够识别导入器外部的序列化数据。 |
needsApplyRevert | 是否需要在 Inspector 中绘制 ApplyRevertGUI 方法。 |
showImportedObject | 是否应将导入的对象显示为单独的编辑器? |
useAssetDrawPreview | 确定资源预览是由 AssetEditor 还是导入器 DrawPreview 处理 |
HasModified | 确定导入设置是否已修改。 |
OnDisable | 当编辑器对象超出范围时调用此函数。 |
OnEnable | 加载对象时调用此函数。 |
OnInspectorGUI | 覆盖此方法以创建您自己的 ScriptedImporter 的 Inpsector GUI。 |
Apply | 将编辑器控件中的任何更改保存到资源的导入设置对象中。 |
ApplyButton | 实现 Inspector 的“应用”按钮。 |
ApplyRevertGUI | 向编辑器添加“应用”和“恢复”按钮。 |
Awake | 启动 Editor 脚本时调用此函数。 |
CanApply | 确定是否可以应用对导入设置的修改。 |
InitializeExtraDataInstance | 此方法在编辑器的初始化过程中调用,在 Awake 之后和 OnEnable 之前。 |
OnApplyRevertGUI | 处理“应用”和“恢复”按钮。 |
RevertButton | 实现 Inspector 的“恢复”按钮。 |
hasUnsavedChanges | 此属性指定在 Inspector 重新构建之前,编辑器是否提示用户保存或放弃未保存的更改。 |
saveChangesMessage | 如果提示用户保存,则显示给用户的消息。 |
serializedObject | 表示正在检查的对象或对象的 SerializedObject。 |
target | 正在检查的对象。 |
targets | 正在检查的所有对象的数组。 |
hideFlags | 对象是否应隐藏、与场景一起保存或用户可修改? |
name | 对象的名称。 |
CreateInspectorGUI | 实现此方法以创建自定义 UIElements Inspector。 |
CreatePreview | 实现此方法以创建自定义 UIElements Inspector 预览。 |
DiscardChanges | 放弃对编辑器内容的未保存更改。 |
DrawDefaultInspector | 绘制内置 Inspector。 |
DrawHeader | 调用此函数以绘制编辑器的标题。 |
DrawPreview | 预览绘制的第一个入口点。 |
GetInfoString | 实现此方法以在资源预览顶部显示资源信息。 |
GetPreviewTitle | 如果要更改预览区域的标签,请覆盖此方法。 |
HasPreviewGUI | 如果在子类中实现了 OnPreviewGUI,则覆盖此方法。 |
OnInteractivePreviewGUI | 实现以创建您自己的交互式自定义预览。交互式自定义预览用于 Inspector 的预览区域和对象选择器。 |
OnPreviewGUI | 为 Inspector 的预览区域、主编辑器的标题和对象选择器创建自定义预览。您必须实现 Editor.HasPreviewGUI 才能调用此方法。 |
OnPreviewSettings | 如果要在预览标题中显示自定义控件,请覆盖此方法。 |
RenderStaticPreview | 如果要渲染静态预览,请覆盖此方法。 |
Repaint | 重新绘制显示此编辑器的任何 Inspector。 |
RequiresConstantRepaint | 检查此编辑器在其当前状态下是否需要持续重新绘制。 |
SaveChanges | 对编辑器内容执行保存操作。 |
UseDefaultMargins | 如果不需要默认边距,请在子类中覆盖此方法以返回 false。 |
GetInstanceID | 获取对象的实例 ID。 |
ToString | 返回对象的名称。 |
ShouldHideOpenButton | 返回 Inspector 中“打开”按钮的可见性设置。 |
CreateCachedEditor | 返回时,previousEditor 是 targetObject 或 targetObjects 的编辑器。该函数要么返回编辑器是否已经在跟踪对象,要么销毁以前的编辑器并创建一个新的编辑器。 |
CreateCachedEditorWithContext | 使用上下文对象创建缓存的编辑器。 |
CreateEditor | 为 targetObject 或 targetObjects 创建自定义编辑器。 |
CreateEditorWithContext | 使用上下文对象为 targetObject 或 targetObjects 创建自定义编辑器。 |
DrawFoldoutInspector | 使用折叠标题为 target 绘制 Inspector GUI。 |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 obj。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 加载新场景时不要销毁目标对象。 |
FindAnyObjectByType | 检索类型为 type 的任何活动的已加载对象。 |
FindFirstObjectByType | 检索类型为 type 的第一个活动的已加载对象。 |
FindObjectsByType | 检索类型为 type 的所有已加载对象的列表。 |
Instantiate | 克隆对象 original 并返回克隆。 |
InstantiateAsync | 捕获原始对象(必须与某些 GameObject 相关)的快照,并返回 AsyncInstantiateOperation。 |
CreateInstance | 创建可脚本化对象的实例。 |
bool | 对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用以查看它们是否引用同一个对象。 |
HasFrameBounds | 验证是否可以为此编辑器计算自定义边界。 |
OnGetFrameBounds | 获取此编辑器目标的自定义边界。 |
OnSceneGUI | 使编辑器能够处理场景视图中的事件。 |
OnDestroy | 销毁可脚本化对象时调用此函数。 |
OnValidate | 仅编辑器函数,当脚本加载或 Inspector 中的值更改时,Unity 会调用此函数。 |
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.