Skip to content

Instantly share code, notes, and snippets.

@yangruihan
Created July 25, 2018 03:26
Show Gist options
  • Select an option

  • Save yangruihan/7bc04f859d54dec032e8fcbfe73dd0f7 to your computer and use it in GitHub Desktop.

Select an option

Save yangruihan/7bc04f859d54dec032e8fcbfe73dd0f7 to your computer and use it in GitHub Desktop.
Unity Editor 获得某一目录下所有Prefab
public static List<GameObject> LoadDirectoryPrefab(string rootPath)
{
var dictoryInfo = new DirectoryInfo(rootPath);
if (!dictoryInfo.Exists) return null;
var ret = new List<GameObject>();
FileInfo[] fileInfos = dictoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories);
EditorUtility.DisplayProgressBar("加载Prefab中...", "开始加载", 0f);
int i = 0;
foreach (FileInfo files in fileInfos)
{
string path = files.FullName;
#if UNITY_STANDALONE_WIN
string assetPath = path.Substring(path.IndexOf("Assets\\"));
#else
string assetPath = path.Substring(path.IndexOf("Assets/"));
#endif
EditorUtility.DisplayProgressBar("加载Prefab中...", assetPath, ++i * 1.0f / fileInfos.Length);
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
ret.Add(prefab);
}
EditorUtility.ClearProgressBar();
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment