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); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961