Patrick Klug
Patrick Klug

Reputation: 14411

Manage prefabs programmatically

Is there a way to find all prefabs defined/available in a Unity project programmatically?

This should include prefabs sitting in the Assets folder which are not in the Resources folder.

Use-cases range from dynamically resolving dependencies (from a name for example) to wanting to implement some central instantiation code.

Upvotes: 1

Views: 340

Answers (1)

David
David

Reputation: 16287

string path = Application.dataFolder;
string[] files = System.IO.Directory.GetFiles(path, "*.prefab");
for(int i=0;i<files.Length;i++)
    Debug.Log(files[i];

enter image description here

If you wish to load the prefab into the scene:

var url = "file://....prefab";
var www = new WWW (url);
yield return www;
www.bytes[]; // This way, you have to convert it to game object yourself.

Upvotes: 1

Related Questions