版本: Unity 6 (6000.0)
语言英语
  • C#

AssetDatabase.ImportAsset

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每项更改建议,并在适用情况下进行更新。

关闭

提交失败

由于某种原因,您的更改建议无法提交。请 <a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static void ImportAsset(string path, ImportAssetOptions options = ImportAssetOptions.Default);

描述

导入路径中的资源。

这将导入指定路径下的资源,并触发一些回调,包括 AssetModificationProcessor.OnWillSaveAssetsAssetPostprocessor.OnPostprocessAllAssets

所有路径相对于项目文件夹,例如: "Assets/MyTextures/hello.png"

其他资源: ImportAssetOptions.

using System.IO;
using UnityEditor;
using UnityEngine;

public class ImportAssetExample : MonoBehaviour { [MenuItem("APIExamples/ImportAsset")] static void ImportAssetOnlyImportsSingleAsset() { string[] fileNames = new[] { "test_file01.txt", "test_file02.txt" };

for (int i = 0; i < fileNames.Length; ++i) { var unimportedFileName = fileNames[i]; var assetsPath = Application.dataPath + "/Artifacts/" + unimportedFileName; File.WriteAllText(assetsPath, "Testing 123"); }

var relativePath = "Assets/Artifacts/test_file01.txt"; AssetDatabase.ImportAsset(relativePath); } }

public class PostProcessImportAsset : AssetPostprocessor { //Based on this example, the output from this function should be: // OnPostprocessAllAssets // Imported: Assets/Artifacts/test_file01.txt // //test_file02.txt should not even show up on the Project Browser //until a refresh happens. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { Debug.Log("OnPostprocessAllAssets");

foreach (var imported in importedAssets) Debug.Log("Imported: " + imported);

foreach (var deleted in deletedAssets) Debug.Log("Deleted: " + deleted);

foreach (var moved in movedAssets) Debug.Log("Moved: " + moved);

foreach (var movedFromAsset in movedFromAssetPaths) Debug.Log("Moved from Asset: " + movedFromAsset); } }