Scripted Importer 是 Unity Scripting API 的一部分。使用Scripted Importer在C# 中编写自定义资源导入器,用于Unity本身不支持的文件格式。
应通过专门定义抽象类 ScriptedImporter 并应用 ScriptedImporter 属性来创建自定义导入器。这样会注册您的自定义导入器以处理一个或多个文件扩展名。当资源管线检测到一个与注册的文件扩展名匹配的文件为新文件或已更改的文件时,Unity 会调用自定义导入器的 OnImportAsset
方法。
注意:Scripted Importer 无法处理已由 Unity 本身处理的文件扩展名。
注意:限制
这是 Scripted Importer 功能的实验性版本,因此仅限于可以使用 Unity Scripting API 创建的资源。这不是此功能在实现或设计方面的限制,而是对其实际使用施加了限制。
下面是 Scripted Importer 的一个简单示例:它使用立方体图元作为主资源,将扩展名为“cube”的资源文件导入 Unity 预制件,并采用了从资源文件中获取的默认材质颜色:
using UnityEngine;
using UnityEditor.Experimental.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"为游戏对象,并将自动转换为预制件
//(只有"主资源"才有资格成为预制件。)
ctx.AddObjectToAsset("main obj", cube);
ctx.SetMainObject(cube);
var material = new Material(Shader.Find("Standard"));
material.color = Color.red;
// 必须为资源分配一个在所有导入之间保持一致的唯一标识符字符串
ctx.AddObjectToAsset("my Material", material);
// 必须销毁未作为导入输出传入上下文的资源
var tempMesh = new Mesh();
DestroyImmediate(tempMesh);
}
}
注意:
ScriptedImporter
属性。ScriptedImporter
基类。OnImportAsset
的 ctx 参数包含导入事件的输入和输出数据。SetMainAsset
的一次(且仅一次)调用。AddSubAsset
的任意次调用。您还可以实现自定义的导入设置编辑器 (Import Settings Editor),为此需要专门定义 ScriptedImporterEditor 类,并使用 CustomEditor
类属性对其进行修饰以告知其用于哪种类型的导入器。
例如:
using UnityEditor;
using UnityEditor.Experimental.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 类添加到项目后,可以像使用 Unity 支持的任何其他本机文件类型一样使用它:
__Alembic__:Alembic Importer 插件已更新为使用 Scripted Importer。有关更多信息,请访问 Unity github:AlembicImporter。
__USD__:USD Importer 插件已更新为使用 Scripted Importer。 有关更多信息,请访问 Unity github:USDForUnity。
2017.1 中的新功能 NewIn20171
2017–07–27 页面已修订
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.