Skip to content

Commit bc8d911

Browse files
committed
added GetAssetNames functionality
1 parent 8a43a17 commit bc8d911

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

Runtime/BundleManager.Api.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static T[] LoadAll<T>(string bundleName) where T : Object
5959
{
6060
EnsureAssetDatabase();
6161
var assets = s_EditorAssetMap.GetAssetPaths(bundleName);
62-
if (assets.Count == 0) return new T[0];
62+
if (assets.Length == 0) return new T[0];
6363

6464
var typeExpected = typeof(T);
65-
var foundList = new List<T>(assets.Count);
65+
var foundList = new List<T>(assets.Length);
6666

67-
for (int i = 0; i < assets.Count; i++)
67+
for (int i = 0; i < assets.Length; i++)
6868
{
6969
var loaded = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(assets[i]);
7070
if (loaded == null) continue;
@@ -223,6 +223,21 @@ public static bool IsAssetExist(string bundleName, string assetName)
223223
if (!s_AssetBundles.TryGetValue(bundleName, out var foundBundle)) return false;
224224
return foundBundle.Bundle.Contains(assetName);
225225
}
226+
227+
public static string[] GetAssetNames(string bundleName)
228+
{
229+
#if UNITY_EDITOR
230+
if (UseAssetDatabase)
231+
{
232+
EnsureAssetDatabase();
233+
return s_EditorAssetMap.GetAssetNames(bundleName);
234+
}
235+
#endif
236+
if (!Initialized) throw new System.Exception("BundleManager not initialized, try initialize first!");
237+
if (!s_AssetBundles.TryGetValue(bundleName, out var foundBundle))
238+
return new string[0];
239+
return foundBundle.Bundle.GetAllAssetNames();
240+
}
226241

227242
public static GameObject Instantiate(GameObject original)
228243
{

Runtime/EditorAssetMap.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class EditorAssetMap
99
{
1010
private Dictionary<string, Dictionary<string, List<string>>> m_Map = new Dictionary<string, Dictionary<string, List<string>>>();
1111
static List<string> s_EmptyStringList = new List<string>();
12+
static string[] s_EmptyStringArray = new string[0];
1213

1314
public EditorAssetMap(AssetbundleBuildSettings settings)
1415
{
@@ -41,10 +42,16 @@ public List<string> GetAssetPaths(string bundleName, string assetName)
4142
return pathList;
4243
}
4344

44-
public List<string> GetAssetPaths(string bundleName)
45+
public string[] GetAssetPaths(string bundleName)
4546
{
46-
if (!m_Map.TryGetValue(bundleName, out var innerDic)) return s_EmptyStringList;
47-
return innerDic.Values.SelectMany(list => list).ToList();
47+
if (!m_Map.TryGetValue(bundleName, out var innerDic)) return s_EmptyStringArray;
48+
return innerDic.Values.SelectMany(list => list).ToArray();
49+
}
50+
51+
public string[] GetAssetNames(string bundleName)
52+
{
53+
if (!m_Map.TryGetValue(bundleName, out var innerDic)) return s_EmptyStringArray;
54+
return innerDic.Keys.ToArray();
4855
}
4956

5057
public bool IsAssetExist(string bundleName, string assetName)

0 commit comments

Comments
 (0)