- Home /
Store all prefabs within an assets folder in an array?
Can it be done, or are there alternatives? UnityScript preferable.
Can you elaborate? Why is that you're trying to do?
Answer by clunk47 · May 28, 2013 at 05:33 PM
Try using Resources.LoadAll
Put your prefabs in Assets/Resources/Prefabs/.
This will find all the game objects in your Prefabs folder, and print the amount of them in the console.
#pragma strict
var prefabs : Object[];
function Start()
{
prefabs = Resources.LoadAll("Prefabs", GameObject);
print(prefabs.Length);
}
Answer by TonyLi · May 28, 2013 at 05:26 PM
Yes, it can be done.
First, use AssetDatabase.LoadAllAssetsAtPath() to load all assets in a folder:
http://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.LoadAllAssetsAtPath.html
Then, for each asset, call PrefabUtility.GetPrefabType():
http://docs.unity3d.com/Documentation/ScriptReference/PrefabUtility.GetPrefabType.html
If the return value is not PrefabType.None, then it's a prefab, so you can add it to an array.
Look through the PrefabType return values in the link above. You may want to include/exclude other return values based on your needs.
Answer by pboechat · May 28, 2013 at 07:38 PM
You could discover all assets in a given folder using AssetDatabase.GetAllAssetPaths () and filter the returned path list.
i.e.:
string[] assetsPaths = AssetDatabase.GetAllAssetPaths ();
foreach (string assetPath in assetsPaths) {
if (assetPath.Contains (yourPrefabsFolderPath)) {
prefabsPaths.Add(assetPath);
}
}
For each filtered path you could load the prefab with Resources.LoadAssetAtPath() and store it in an array.