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.
다음은 스크립트된 임포터의 간단한 예제입니다. 이 예제에서는 확장자가 “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);
}
}
참고:
ScriptedImporter
속성을 CubeImporter 클래스에 배치하여 에셋 파이프라인에 등록됩니다.ScriptedImporter
기본 클래스(base class)를 구현합니다.OnImportAsset
의 ctx 인수는 임포트 이벤트의 입력 및 출력 데이터를 모두 포함합니다.SetMainAsset
호출을 하나만 생성해야 합니다.AddSubAsset
호출을 필요한 수만큼 생성할 수 있습니다.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();
}
}
스크립트된 임포터 클래스를 프로젝트에 추가한 후 Unity 에디터에서 지원되는 기타 네이티브 파일 유형과 똑같이 사용할 수 있습니다.
Alembic: Alembic 임포터 플러그인은 스크립트된 임포터를 사용하도록 업데이트되었습니다. 자세한 내용은 Unity github: AlembicImporter 페이지를 참조하십시오.
USD: USD 임포터 플러그인은 스크립트된 임포터를 사용하도록 업데이트되었습니다. 자세한 내용은 Unity github:: USDForUnity 페이지를 참조하십시오.