- Home /
How can i add all the prefabs in the assets directory and sub directories to List or Array ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class Prefabs : MonoBehaviour
{
List<GameObject> prefabs = new List<GameObject>();
// Use this for initialization
void Start ()
{
var resourcesPath = Application.dataPath;
var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
foreach (var absolutePath in absolutePaths)
{
var prefab = Path.GetFileName(absolutePath);
prefabs.Add(prefab as GameObject);
}
}
// Update is called once per frame
void Update () {
}
}
I don't want to get all the prefabs from the resources but from the assets. So this is what i'm doing in the loop i'm getting all the prefabs names from the assets folder and sub directories.
I don't want to use Resources.Load or LoadAll since i'm getting all the prefabs under assets directory.
The problem is that each prefab is a string not GameObject so i can't add it to the List.
The assets folder does not exist during runtime, so technically this is only possible during editor. I'm not sure why you can't use the resources folder. What are you actually trying to do?
I don't have a Resources folder at least not under Assets. But under Assets folder i have a package i imported and this package have some prefabs and it's taking time to add a lot of prefabs to the terrain for example tress and bushes.
What i want to do is to get all the prefabs and either add them to the terrain and then in the editor to use them or via the script it self to use the prefabs to add the trees bushes and other stuff to the terrain already.
It's just adding each tree all the time to the terrain trees and then to add the trees to the terrain in the scene take time.
So i think the second way is better. To get all prefabs in the script and then in the script to put prefabs already in the terrain in the scene view.
The problem is that i don't have resources folder in the project and the prefabs i want are in a packager folder under assets.
So what you want is to load Trees and Details from the AssetDatabase, then add them to your terrains detailProttypes and treePrototypes so you can start painting trees and bushes?
Your answer
Follow this Question
Related Questions
Save GameObject to file without Playerpref 0 Answers
Save GameObject to file without Playerpref 2 Answers
Why when creating new GameObjects I can't move them in the editor when the game is running ? 1 Answer
Is it right to turn false both navmeshagent updateRotation and updatePosition properties ? 0 Answers
Why when using LookAt to make the turret facing the target it's facing the other direction ? 1 Answer