Version: 2017.1
渐进光照贴图 (Progressive Lightmapper)
Look Dev

脚本化导入器 (Scripted Importer)

Scripted Importer 是 Unity Scripting API 的一部分。使用Scripted Importer在C# 中编写自定义资源导入器,用于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.

注意:Scripted Importer 无法处理已由 Unity 本身处理的文件扩展名。

注意:限制

这是 Scripted Importer 功能的实验性版本,因此仅限于可以使用 Unity Scripting API 创建的资源。这不是此功能在实现或设计方面的限制,而是对其实际使用施加了限制。

示例

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

using UnityEditor.Experimental.AssetImporters;

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

    public override void OnImportAsset(AssetImportContext ctx)
    {
        // Main asset
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        ctx.SetMainAsset("MainAsset", cube);
        
        // Material as sub-asset
        string text = File.ReadAllText(ctx.assetPath, Encoding.UTF8);
        var channels = text.Split(',');
        var color = new Color(float.Parse(channels[0]) + m_ColorShift,
                              float.Parse(channels[1]) + m_ColorShift, 
                              float.Parse(channels[2]) + m_ColorShift);
        
        var material = new Material(Shader.Find("Standard")) { color = color };
        cube.GetComponent<Renderer>().material = material;
        ctx.AddSubAsset("Material", material);
    }
}

注意

  • 导入器在Unity的资源管线中注册,是通过在CubeImporter类上放置ScriptedImporter属性。
  • CubeImporter 类实现了抽象的 ScriptedImporter 基类。
  • OnImportAsset 的 ctx 参数包含导入事件的输入和输出数据。
  • 每个导入事件必须生成对 SetMainAsset 的一次(且仅一次)调用。
  • 每个导入事件可以根据需要生成对 AddSubAsset 的任意次调用。
  • 请参阅脚本 API 文档以了解更多详细信息。

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.Experimental.AssetImporters;

[CustomEditor(typeof(CubeImporter))]
public class CubeImporterEditor: ScriptedImporterEditor
{
    public override void OnInspectorGUI()
    {
        var prop = serializedObject.FindProperty("m_ColorShift");
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Color Shift");
        EditorGUILayout.PropertyField(prop )); 
        EditorGUILayout.EndHorizontal();
        // Important: call this at end!
        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 的实际使用




渐进光照贴图 (Progressive Lightmapper)
Look Dev