#if UNITY_EDITOR using AssetBundleBrowser.AssetBundleDataSource; using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; using AssetBundleDataSource = AssetBundleBrowser.AssetBundleDataSource;
privatestaticvoidEnterPlay(PlayModeStateChange playModeStateChange) { if (playModeStateChange == PlayModeStateChange.EnteredPlayMode) { var m_DataSourceList = new List<AssetBundleDataSource.ABDataSource>();
foreach (var info inBuildCustomABDataSourceList()) { m_DataSourceList.AddRange(info.GetMethod("CreateDataSources").Invoke(null, null) as List<AssetBundleDataSource.ABDataSource>); }
if (m_DataSourceList.Count > 1) { // 载入所有包信息 curData = m_DataSourceList[0]; } else curData = AssetBundleBrowser.AssetBundleModel.Model.DataSource; packetHash = new HashSet<string>(); foreach (var i in curData.GetAllAssetBundleNames()) { packetHash.Add(i); } } }
privatestatic List<Type> BuildCustomABDataSourceList() { var properList = new List<Type>(); properList.Add(null); //empty spot for "default" var x = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in x) { try { var list = new List<Type>( assembly .GetTypes() .Where(t => t != typeof(ABDataSource)) .Where(t => typeof(ABDataSource).IsAssignableFrom(t)));
for (int count = 0; count < list.Count; count++) { if (list[count].Name == "AssetDatabaseABDataSource") properList[0] = list[count]; elseif (list[count] != null) properList.Add(list[count]); } } catch (System.Exception) { //assembly which raises exception on the GetTypes() call - ignore it } }
return properList; }
publicstatic T LoadRes<T>(string packetName, string resName) where T : UnityEngine.Object { if (packetHash.Contains(packetName)) { foreach (var i in curData.GetAssetPathsFromAssetBundle(packetName)) { // 全匹配或者文件名匹配 if (resName.Equals(i) || Path.GetFileNameWithoutExtension(i).Equals(resName)) { var obj = AssetDatabase.LoadAssetAtPath<T>(i); if (obj != null) return obj; } } } else Debug.LogError($"AB包中不存在{packetName}这个包名"); returnnull; } } }