Hello! I am working on a small programme created to scan all assets in a Unity project and relocate them to their correct directories if they are misplaced, however I’m stuck due to a persistent problem “Parent directory is not in asset database” in my Unity Editor script. This occurs whenever I use AssetDatabase.MoveAsset, even though I’m taking steps to proactively create the folder.
What I’ve done:
- Folder Creation: Using Directory.CreateDirectory or custom methods built upon AssetDatabase.CreateFolder.
- Asset Import: Calling AssetDatabase.ImportAsset with the ImportAssetOptions.ImportRecursive flag.
- Asset Database Refresh: Frequent use of AssetDatabase.Refresh.
Snippet (Illustrative):
public class AssetData
{
public string Path { get; private set; }
public string Name { get; private set; }
public string SuggestedPath { get; private set; }
public string FileExtension
{
get { return System.IO.Path.GetExtension(Path).ToLower(); }
}
public bool IsModified { get; set; } = false;
public AssetData(string path, string name, string suggestedPath)
{
Path = path;
Name = name;
SuggestedPath = suggestedPath;
}
}
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class AssetProcesser : MonoBehaviour
{
private Queue<AssetData> m_assetsToProcess;
// Here List<AssetData> assets has one element and it's value is..
// asset.Name = "Ground"
// assets.Path = "Assets/Prefab/Ground.mat"
// asset.SuggestedPath = "Assets/NewMaterial/Ground.mat"
public void CorrectAssets(List<AssetData> assets)
{
m_assetsToProcess = new Queue<AssetData>(assets);
EditorApplication.update += ProcessAssetCorrectionQueue;
AssetDatabase.StartAssetEditing();
}
private void ProcessAssetCorrectionQueue()
{
if (m_assetsToProcess.Count == 0)
{
FinishAssetCorrection();
return;
}
try
{
foreach (AssetData asset in m_assetsToProcess)
{
ProcessAsset(asset);
}
}
finally
{
FinishAssetCorrection();
}
}
private void ProcessAsset(AssetData asset)
{
string guid = AssetDatabase.AssetPathToGUID(asset.Path);
string currentPath = AssetDatabase.GUIDToAssetPath(guid);
string newPath = asset.SuggestedPath;
string newParentDirectory = Path.GetDirectoryName(newPath);
CreateFolderIfNeeded(newParentDirectory);
ImportFolderRecursive(newParentDirectory);
AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
string
error = AssetDatabase.MoveAsset(currentPath,
newPath); // Error I am getting : Parent directory is not in asset database
if (!string.IsNullOrEmpty(error))
{
ToolsLogs.LogError($"Failed to rename asset {asset.Path}: {error}");
}
AssetDatabase.ImportAsset(newPath, ImportAssetOptions.ImportRecursive);
}
private static void CreateFolderIfNeeded(string folderPath)
{
if (!folderPath.StartsWith("Assets/"))
{
ToolsLogs.LogError("Invalid path. Path must be within the Assets folder.");
return;
}
string[] folders = folderPath.Substring("Assets/".Length).Split('/');
string currentPath = "Assets";
if (!AssetDatabase.IsValidFolder(folderPath))
{
try
{
for (int i = 0; i < folders.Length; i++)
{
string folderName = folders[i];
currentPath = Path.Combine(currentPath, folderName);
if (AssetDatabase.IsValidFolder(currentPath))
{
continue;
}
string createdFolderGuid = AssetDatabase.CreateFolder(Path.GetDirectoryName(currentPath),
Path.GetFileName(currentPath));
if (!string.IsNullOrEmpty(createdFolderGuid))
{
AssetDatabase.ImportAsset(currentPath, ImportAssetOptions.ImportRecursive);
Debug.Log($"Created folder: {createdFolderGuid}");
}
}
}
catch (Exception ex)
{
Debug.LogError($"Error creating directory: {ex.Message}");
}
}
}
private static void ImportFolderRecursive(string folderPath)
{
AssetDatabase.ImportAsset(folderPath,
ImportAssetOptions.ImportRecursive | ImportAssetOptions.ForceSynchronousImport);
}
private void FinishAssetCorrection()
{
EditorUtility.ClearProgressBar();
EditorApplication.update -= ProcessAssetCorrectionQueue;
AssetDatabase.Refresh();
}
}
here this
Things I’ve Tried:
- Debugging with Logs: I’ve verified the folder exists on the file system, but the Asset Database doesn’t recognize it.
- Forced Delays: Tried add delay but no help.
- Alternative Folder Creation: Experimented with other folder creation methods(Directory.CreateDirectory)
Questions:
- What are the typical reasons the Asset Database might lag in recognizing new folders?
- Are there more reliable ways to force synchronization between the file system and the Asset Database?
- Could other aspects of my asset processing script be causing interference?
Environment:
- Unity Version: [2021.3.15f1]
- Operating System: [macOS]
Additional Notes:
- I’m open to exploring meta file issues if basic solutions don’t work.
- The script executes only within the Unity Editor (not at runtime). The code provided is simplified.
Thanks in advance for any guidance!