AssetImporterEditor
UnityEditor.AssetImporters 中的类
/
继承自:Editor
建议修改
成功!
感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每项建议修改,并在适用时进行更新。
关闭
提交失败
由于某些原因,您的建议修改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。
关闭
使用默认编辑器编辑资源的导入设置。您可以为特定资源类型定义自定义导入设置编辑器。为此,创建一个继承自 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);
}
}
以下示例演示了如何在自定义 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)
{
// ...
}
}