自定义资源导入器的抽象基类。
脚本化导入器是与特定文件扩展名关联的脚本。Unity 的资源管线调用它们来将关联文件的内容转换为资源。
使用 ScriptedImporterAttribute 类可向资源管线注册自定义导入器。
The C# fields of a ScriptedImporter are serialized, exactly like fields on a MonoBehaviour. See Script Serialization for details.
You can see these properties in the Inspector and use them to control the behaviour of the importer for each asset.
To programmatically access the value of an asset's properties, use AssetImporter.GetAtPath and type cast the return value to the correct class derived from ScriptedImporter.
After changing values, trigger a fresh import by calling EditorUtility.SetDirty and then AssetImporter.SaveAndReimport.
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); } }
OnImportAsset | 此方法必须由派生类重写,并由资源管线调用来导入文件。 |
SupportsRemappedAssetType | 如果 ScriptedImporter 支持重新映射特定资源类型,则重载此方法。 |
GatherDependenciesFromSourceFile | A static callback that you can implement to set up artifact dependencies to other Assets, and optimize the order your assets are imported. |
OnValidate | 加载导入器后或 Inspector 中的值发生更改时,将调用此函数。 |
Reset | 重置为默认值。 |
assetBundleName | 获取或设置 AssetBundle 名称。 |
assetBundleVariant | 获取或设置 AssetBundle 变体。 |
assetPath | 此导入器的资源的路径名称。(只读) |
importSettingsMissing | 如果导入的资源未随附任何元文件,则此值为 true。 |
userData | 获取或设置任意用户数据。 |
hideFlags | 该对象应该隐藏、随场景一起保存还是由用户修改? |
name | 对象的名称。 |
AddRemap | 将子资源从导入的资源(例如 FBX 文件)映射到同类型的外部资源。 |
GetExternalObjectMap | 获取 AssetImporter 使用的外部对象映射的副本。 |
RemoveRemap | 从外部对象的映射中删除项。 |
SaveAndReimport | 如果资源导入器已标记为“脏”,则保存资源导入器设置。 |
SetAssetBundleNameAndVariant | 设置 AssetBundle 名称和变体。 |
GetInstanceID | Gets the instance ID of the object. |
ToString | 返回对象的名称。 |
GetAtPath | 在 path 处检索资源的资源导入器。 |
GetImportLog | Retrieves logs generated during the import of the asset at path. |
Destroy | 移除 GameObject、组件或资源。 |
DestroyImmediate | 立即销毁对象 /obj/。强烈建议您改用 Destroy。 |
DontDestroyOnLoad | 在加载新的 Scene 时,请勿销毁 Object。 |
FindAnyObjectByType | Retrieves any active loaded object of Type type. |
FindFirstObjectByType | Retrieves the first active loaded object of Type type. |
FindObjectsByType | Retrieves a list of all loaded objects of Type type. |
Instantiate | 克隆 original 对象并返回克隆对象。 |
bool | 该对象是否存在? |
operator != | 比较两个对象是否引用不同的对象。 |
operator == | 比较两个对象引用,判断它们是否引用同一个对象。 |