- Home /
Editor Script: Getting all the models in a folder.
I want to make an editor script that will generate gameobject prefabs out of all models in a specified directory.
I got the menu GUI all setup, but I'm not sure how to do the logic. Specifically, cycling through all models in a directory (including child directories).
I'm guessing there's a method to get all assets in a directory, but I don't know hot to specify only models. I assume I could at least load all assets in a directory and then check if they have mesh renderer components. I just don't know what method that would be to get all assets in a directory, and haven't had any luck finding it.
Also what type of asset would model files be? (Imported .blend, .fbx, etc) I guess GameObject?
Answer by Nido · Jul 28, 2015 at 09:49 AM
string sAssetFolderPath = "Assets/Models";
string[] aux = sAssetFolderPath.Split(new char[] { '/' });
string onlyFolderPath = aux[0] + "/" + aux[1] + "/";
string[] aFilePaths = Directory.GetFiles(onlyFolderPath);
foreach (string sFilePath in aFilePaths)
{
if (Path.GetExtension(sFilePath) == ".FBX" || Path.GetExtension(sFilePath) == ".fbx")
{
Debug.Log(Path.GetExtension(sFilePath));
Object objAsset = AssetDatabase.LoadAssetAtPath(sFilePath, typeof(Object));
//Here try something like INSTANTIATE(objAsset);
}
}
This returns you all models (specify extension in the IF statement). Then try to instantiate them or look for a "make prefab" solution in Editor. I'm waiting to know if this works, comment please ;)
Edit: The split in the path is because I was using this on a SelectedObject. But this version is modifyed for entire folder "Models" without selecting any object.
Answer by ruchirnaphade · Nov 09, 2020 at 10:09 AM
@PsychoDuckArcade ... I tried above code and I got the .fbx files from my folders. I want to make one fbx file child of another empty gameobject in my scene (Drag and drop functionality) but I am getting a below error:
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'Model').
Do you have any idea why it is like this?
Answer by Jeff-Kesselman · Nov 30, 2020 at 08:38 PM
You could create a file watcher in C# foir your given directory that runs asynchronously and calls you back, or sets a flag you can check in update if you need to do something on the update thread.
There are plenty of exampels for C# file watchers out there. Do a google.