I'm attempting to do a simple import from a local path. Not sure where I'm messing up here. I'm not the strongest coder, so any insight is appreciated. Below is my attempt.
[MenuItem("ImportMenu/ImportAsset")]
static void ImportAssetOnlyImportsSingleAsset()
{
// My asset package file name
string fileName = "MyAsset.unitypackage";
// Project Asset folder path to save file to
var assetsPath = Application.dataPath "/" fileName;
Debug.Log($"Application.dataPath: {Application.dataPath}");
// Where my asset package is located
var filePath = "D:/Local_Projects/Packages/MyAsset";
// Import asset
AssetDatabase.ImportAsset(filePath);
//Debug.Log("Imported: " importedAssets);
}
CodePudding user response:
AssetDatabase can only load from files from the root of the Project. These are Assets or Packages. If your project is not directly in Local_Projects it would not work.
From documentation
All paths are relative to the project folder, for example: "Assets/MyTextures/hello.png"
What you can do is copy the content of this package into the directory of the project.
File.Copy(fileToCopy, destinationFile); // or File.Move(...)
AssetDatabase.ImportAsset(destinationFile);
// Do something with this asset
// Optionaly
File.Remove(destinationFile);
