Version: 2020.2
AssetDatabase 批处理
Importer Consistency

脚本化导入器 (Scripted Importer)

Scripted Importers are part of the Unity Scripting API. You can use Scripted Importers to write custom Asset importers in C# for file formats not natively supported by Unity.

Create a custom importer by specializing the abstract class ScriptedImporter and applying the ScriptedImporter attribute. This registers your custom importer to handle one or more file extensions. When a file matching the registered file extensions is detected by the Asset pipeline as being new or changed, Unity invokes the method OnImportAsset of your custom importer.

Note: Scripted Importers cannot handle a file extension that is already natively handled by Unity.

示例

下面是 Scripted Importer 的一个简单示例:它使用立方体图元作为主资源,将扩展名为“cube”的资源文件导入 Unity 预制件,并采用了从资源文件中获取的默认材质颜色:

using UnityEngine;
using UnityEditor.AssetImporters;
using System.IO;

[ScriptedImporter(1, "cube")]
public class CubeImporter : ScriptedImporter
{
    public float m_Scale = 1;

    public override void OnImportAsset(AssetImportContext ctx)
    {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        var position = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath));

        cube.transform.position = position;
        cube.transform.localScale = new Vector3(m_Scale, m_Scale, m_Scale);

        // 'cube' is a a GameObject and will be automatically converted into a prefab
        // (Only the 'Main Asset' is elligible to become a Prefab.)
        ctx.AddObjectToAsset("main obj", cube);
        ctx.SetMainObject(cube);

        var material = new Material(Shader.Find("Standard"));
        material.color = Color.red;

        // Assets must be assigned a unique identifier string consistent across imports
        ctx.AddObjectToAsset("my Material", material);

        // Assets that are not passed into the context as import outputs must be destroyed
        var tempMesh = new Mesh();
        DestroyImmediate(tempMesh);
    }
}

注意

  • 导入器在Unity的资源管线中注册,是通过在CubeImporter类上放置ScriptedImporter属性。
  • CubeImporter 类实现了抽象的 ScriptedImporter 基类。
  • OnImportAsset 的 ctx 参数包含导入事件的输入和输出数据。
  • 每个导入事件必须生成对 SetMainAsset 的一次(且仅一次)调用。
  • 每个导入事件可以根据需要生成对 AddSubAsset 的任意次调用。
  • Please refer to the Scripting API documentation for more details.

You may also implement a custom Import Settings Editor by specializing ScriptedImporterEditor class and decorating it with the class attribute CustomEditor to tell it what type of importer it is used for.

例如:

using UnityEditor;
using UnityEditor.AssetImporters;
using UnityEditor.SceneManagement;
using UnityEngine;

[CustomEditor(typeof(CubeImporter))]
public class CubeImporterEditor: ScriptedImporterEditor
{
    public override void OnInspectorGUI()
    {
        var colorShift = new GUIContent("Color Shift");
        var prop = serializedObject.FindProperty("m_ColorShift");
        EditorGUILayout.PropertyField(prop, colorShift);
        base.ApplyRevertGUI();
    }
}

使用 Scripted Importer

将 Scripted Importer 类添加到项目后,可以像使用 Unity 支持的任何其他本机文件类型一样使用它:

  • 将一个受支持文件拖放到资源目录层级视图中可导入该文件。
  • 重新启动 Unity Editor 会重新导入自上次更新以来发生更改的所有文件。
  • 在磁盘上编辑资源文件并返回 Unity Editor 会触发重新导入。
  • 使用 Asset > Import New Asset… 导入新资源。
  • 通过菜单 Asset > Reimport 显式触发重新导入。
  • 单击资源可在 Inspector 窗口中查看其设置。要修改其设置,请在 Inspector 窗口中编辑这些设置,然后单击 Apply

Scripted Importer 导入的资源 (An Alembic Girl) 的 Inspector 窗口
Scripted Importer 导入的资源 (An Alembic Girl) 的 Inspector 窗口

Scripted Importer 的实际使用

AssetDatabase 批处理
Importer Consistency