- Home /
List all prefabs in project with a specific name
Hi
I have various prefabs scattered around my project that I need to modify at runtime. I need a list of their file path's so I can load and modify them.
All of them have a "@modifyMe" in their names but they may not necessarily be already loaded so doing a simple "GameObject.Find" wont work in this case.
Any ideas?
Answer by ArkaneX · Oct 29, 2013 at 05:40 PM
You can use Directory.GetFiles method to get absolute paths of your prefabs. You can then convert them to paths relative to your Resources folder, remove extensions and load prefabs. Sample code, tested on PC:
var resourcesPath = System.IO.Path.Combine(Application.dataPath, "Resources");
var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*@modifyMe*.prefab", System.IO.SearchOption.AllDirectories);
foreach (var absolutePath in absolutePaths)
{
var path = absolutePath.Replace(resourcesPath + System.IO.Path.DirectorySeparatorChar, "");
path = path.Substring(0, path.Length - 7);
path = path.Replace("\\", "/");
var prefab = Resources.Load(path);
// process loaded prefab
}
Your answer
Follow this Question
Related Questions
Find all of the prefabs in the project that have a specific component 0 Answers
How to check what prefabs exist in one of the project folders? (not necessarily in Resources) 1 Answer
Get object/prefab name as string. 1 Answer
Issues with tracking prefab instances and gameobject naming (JS) 1 Answer
How to check if an object is asset in build runtime (Not in Editor) ? 4 Answers