Version: Unity 6.0 (6000.0)
言語 : 日本語
アセットを同時にインポートする
テキストアセット

Scripted Importer

Scripted Importer は Unity スクリプティング API の一部です。Scripted Importer を使用すると C# でカスタムアセットインポーターを作成できます。これにより、Unity でネイティブにサポートされていないファイル形式の独自のサポートを追加できます。

カスタムインポーターを作成するには、抽象クラス ScriptedImporter を特殊化し、ScriptedImporter 属性を適用します。これにより、カスタムインポーターが登録され、1 つ以上のファイル拡張子が処理されます。登録されたファイル拡張子に一致する新規または変更されたファイルが Asset パイプラインによって検出されると、Unity はカスタムインポーターのメソッド OnImportAsset を呼び出します。

注意:Scripted Importer は、すでに 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 GameObject and will be automatically converted into a prefab
        // (Only the 'Main Asset' is eligible 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);
    }
}

ノート:

  • CubeImporter クラスに ScriptedImporter 属性を配置することによって、インポーターは Unity のアセットパイプラインに登録されます。
  • CubeImporter クラスは抽象クラス ScriptedImporter の基本クラスを実装します。
  • OnImportAsset の ctx 引数には、インポートイベントの入力データと出力データの両方が含まれます。
  • 各インポートイベントは、SetMainAsset への呼び出しを 1 回 (1 回のみ) 発生させる必要があります。
  • 各インポートイベントは、必要な回数の AddSubAsset の呼び出しを生成できます。
  • 詳細については、スクリプティング API のドキュメントを参照してください。

また、ScriptedImporterEditor クラスを特殊化し、そのクラスをクラス属性 CustomEditor で修飾してどのタイプのインポーターに対して使用されるかを指示ことにより、カスタムインポート設定エディターを実装することもできます。

例:

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 エディターを再起動すると、最後の更新以降に変更したファイルすべてが再インポートされます。
  • アセットファイルをディスク上で編集して Unity エディターに戻ると、再インポートが開始されます。
  • Asset > Import New Asset… を使用して新しいアセットをインポートします。
  • 明示的にメニュー (Asset > Reimport) から再インポートをトリガーします。
  • アセットをクリックすると、Inspector ウィンドウにアセットの設定が表示されます。設定を変更するには、Inspector ウィンドウで編集し Apply をクリックします。

Scripted Importer によってインポートされたアセット (An Alembic Girl) の Inspector ウィンドウ
Scripted Importer によってインポートされたアセット (An Alembic Girl) の Inspector ウィンドウ

Scripted Importer の実際の使用例

  • Alembic:Alembic インポータープラグインは Scripted Importer を使用するように更新されています。詳細については、Unity GitHub: AlembicImporter を参照してください。

  • USD: USD インポータープラグインは、Scripted Importer を使用します。詳細については、USD Importer パッケージのドキュメントを参照してください。

アセットを同時にインポートする
テキストアセット