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.
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
}