I’m using Flax Engine to visualise some data in a file. This is run entirely in the editor. When I load the file I delete all of my existing materials, to permit a reload of the file into a fresh environment:
string folder = "MuJoCoEd/Materials";
private static void DeleteProjectContentsFiles(string folder)
{
Editor.Instance.ContentDatabase.Rebuild(true);
ContentFolder materialsFolder = Editor.Instance.ContentDatabase.Find(Path.Combine(Globals.ProjectContentFolder, folder)) as ContentFolder;
List<ContentItem> itemsToDelete = [];
if (materialsFolder != null)
{
foreach (ContentItem item in materialsFolder.Children)
{
itemsToDelete.Add(item);
}
foreach (ContentItem item in itemsToDelete)
{
Editor.Instance.ContentDatabase.Delete(item);
}
}
else
{
Debug.LogWarning("Material deletion failed");
}
Editor.Instance.ContentDatabase.Rebuild(true);
}
And I see an output log reflecting this:
[Info] Deleting asset FlaxEngine.Material, f820e8d74fe28897c503eb9f8a6757f3, D:\FlaxProjects/MuJoCoEd/Content/MuJoCoEd/Materials/black.flax…
However, when I attempt to create the material using:
string Pname = "black"
private void CreateMaterial()
{
Editor.Instance.ContentDatabase.Rebuild(true);
string localPath = "Content/MuJoCoEd/Materials/" + Pname + ".flax";
if(Editor.CreateAsset("Material", localPath))
{
throw new Exception("Failed to create new asset.");
}
}
I get an exception thrown and I can see
[Info] Asset already exists. Using old ID: f820e8d74fe28897c503eb9f8a6757f3
[Warning] Cannot move imported file C:\Users/***/AppData/Local/Temp/678bf465-4be6-5c21-e238-4396df38f6ce/44158ab1491c91d9e19cd98382416c87.flax to the destination path Content/MuJoCoEd/Materials/black.flax
After the exception is thrown, I then see:
[Info] Deleting asset ‘D:\FlaxProjects/MuJoCoEd/Content/MuJoCoEd/Materials/black.flax’:f820e8d74fe28897c503eb9f8a6757f3.
So apparently the asset deletion has not been fully processed before the asset creation occurs, even though I force a content database update with Editor.Instance.ContentDatabase.Rebuild(true);
Any thoughts on this or a better strategy for cleaning out an asset directory and dynamically creating new material assets in it based on some parameters in an imported xml file would be much appreciated.
Edit: I guess this could just be a delay in the file system, if so, what would be a good wait until ready strategy?