Hi all,
So I am writing an Editor script that catches fbx files on import and performs various functions on them during the OnPreprocess and OnPostprocess phase. One of the tasks I am attempting to perform is to change the filepath on import. The art team controls where files exist in the file path, so they will be putting a subfolder in the Custom User Properties of a model, which this script will then read. Once it reads it, the script should check to see if the desired file path exists, and if not, create it. Afterwards, it should move the asset to the new file path. At this point I have it creating the new file path, but it does not register with the AssetDatabase and I always get the following error:
ERROR: Parent directory is not in asset database
I am including sample code here to show what I am attempting. I have removed all code that is irrelevant to this specific task.
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class CustomAssetImporter : AssetPostprocessor
{
// contains the variables placed directly
// on the model by the artists
private static Dictionary<string, string> _modelInfo;
static void OnPostprocessAllAssets( string[] _imported,
string[] _deleted,
string[] _moved,
string[] _fromPath )
{
// for each imported item...
foreach(string item in _imported)
{
// ...if the imported item is a model...
if( item.EndsWith(".fbx") || item.EndsWith(".FBX") )
{
// ...get the filepath...
string finalPath = CustomAssetImporter.getPath(item);
// ... and then move the incoming fbx
// this is where it breaks. It always returns the following:
// ERROR: Parent directory is not in asset database
// even though I can look at the project file structure
// and see that the folders exist. wtf?
string error = AssetDatabase.MoveAsset(item, finalPath);
Debug.Log ("ERROR: " + error);
}
}
}
private static string getPath(string _asset)
{
string returnInfo = "";
string path = "Assets/Objects/";
// get the path of the incoming model
string[] incomingPath = _asset.Split( new char[] {'/'},
StringSplitOptions.None);
// modify the path string
// subfolder is a variable placed on the fbx by the artist
if(CustomAssetImporter._modelInfo["subfolder"] != null)
{
path += CustomAssetImporter._modelInfo["subfolder"];
}
path += "/" + incomingPath[incomingPath.Length - 1];
// create file structure if it doesn't exist
// and return the final path
returnInfo = CustomAssetImporter.getPathFromString(path);
return returnInfo;
}
public static string getPathFromString(string _path)
{
string[] oldPath = _path.Split( new char[] {'/'},
StringSplitOptions.None);
string newPath = "Assets";
for(int i = 0; i < oldPath.Length; i++)
{
// if not Assets... We can skip Assets. If Assets doesn't
// exist, we have a whole different issue altogether
if(oldPath *!= "Assets")*
-
{*
-
// if not an actual file...*
_ if(oldPath*.IndexOf(‘.’) == -1)_
_ {_
_ // if the directory does not exist…_
_ if( !Directory.Exists(newPath + “/” + oldPath) )
{
// create the directory underneath parent…
string guid = AssetDatabase.CreateFolder(newPath,
oldPath);
newPath += “/” + path;
}
else*
* {
// if it does exist, move to the*
* // next part of the path…
newPath += “/” + path;
}
}
}
}*_
* // step forward one frame. This is the most recent*
* // test to try to get the new file structure to*
* // register in the AssetDatabase*
* EditorApplication.Step();*
* return newPath + “/”;*
* }*
}
Essentially, as i understand it what is happening is that while the file path is being created, for some reason it is not registering in the AssetDatabase. I have tried AssetDatabase.Refresh() already, as well as a number of random things I have found elsewhere, to no avail.
Thank you in advance for any advice or assistance. If more information is needed, I will provide whatever I can.
-Jeremy