public T[] LoadAllAssets <T>(GameAssetStruct gas) where T : UnityEngine.Object { #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(gas.BundleFullName); T[] result = new T[assetPaths.Length]; if (assetPaths.Length == 0) { Log(LogType.Error, "There is no asset with name \"" + gas.AssetName + "\" in " + gas.BundleFullName); return(null); } for (int i = 0, length = assetPaths.Length; i < length; i++) { // @TODO: Now we only get the main object from the first asset. Should consider type also. Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[i]); AssetBundleLoadAssetOperationSimulation operation = new AssetBundleLoadAssetOperationSimulation(target, null); result[i] = operation.GetAsset <T>(); } return(result); } else #endif { AssetBundle bundle = LoadOrGetAssetsBundle(gas); if (bundle != null) { return(bundle.LoadAllAssets <T>()); } return(null); } }
public AssetBundleLoadAssetOperation LoadAssetAsync <T>(GameAssetStruct gas, System.Action <T> pCallBack) where T : UnityEngine.Object { Log(LogType.Info, "Loading " + gas.AssetName + " from " + gas.BundleFullName + " bundle"); AssetBundleLoadAssetOperation operation = null; #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(gas.BundleFullName, gas.AssetName); if (assetPaths.Length == 0) { Log(LogType.Error, "There is no asset with name \"" + gas.AssetName + "\" in " + gas.BundleFullName); return(null); } // @TODO: Now we only get the main object from the first asset. Should consider type also. //Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]); T target = AssetDatabase.LoadAssetAtPath <T>(assetPaths[0]); operation = new AssetBundleLoadAssetOperationSimulation(target, null); pCallBack(operation.GetAsset <T>()); } else #endif { LoadAssetBundleAsy(gas.BundleFullName); operation = new AssetBundleLoadAssetOperationFull(gas.BundleFullName, gas.AssetName, typeof(T), a => { pCallBack(a.GetAsset <T>()); }); m_InProgressOperations.Add(operation); } return(operation); }
// Unload assetbundle and its dependencies. public void UnloadAssetBundle(GameAssetStruct gas) { #if UNITY_EDITOR // If we're in Editor simulation mode, we don't have to load the manifest assetBundle. if (SimulateAssetBundleInEditor) { return; } #endif //UDebug.Log(m_LoadedAssetBundles.Count + " assetbundle(s) in memory before unloading " + assetBundleName); UnloadAssetBundleInternal(gas.BundleFullName); UnloadDependencies(gas.BundleFullName); //UDebug.Log(m_LoadedAssetBundles.Count + " assetbundle(s) in memory after unloading " + assetBundleName); }
private AssetBundle LoadOrGetAssetsBundle(GameAssetStruct gas) { if (string.IsNullOrEmpty(gas.AssetName)) { Debug.LogError("gas.BundleFullName:" + gas.BundleFullName + " gas.AssetName:" + gas.AssetName); return(null); } AssetBundle bundle; LoadedAssetBundle loadedBundle = null; if (m_LoadedAssetBundles.TryGetValue(gas.BundleFullName, out loadedBundle)) { bundle = loadedBundle.m_AssetBundle; } else { bundle = LoadAssetsBundle(gas.BundleFullName); } return(bundle); }
/// <summary> /// 同步加载某种类型资源 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="gas"></param> /// <returns></returns> public T LoadAsset <T>(GameAssetStruct gas) where T : UnityEngine.Object { #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(gas.BundleFullName, gas.AssetName); if (assetPaths.Length == 0) { Log(LogType.Error, "There is no asset with name \"" + gas.AssetName + "\" in " + gas.BundleFullName); return(null); } // @TODO: Now we only get the main object from the first asset. Should consider type also. //Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]); T t = AssetDatabase.LoadAssetAtPath <T>(assetPaths[0]); return(t); //下面的写法有bug,GetAsset里强制类型转换,不对 //AssetBundleLoadAssetOperationSimulation operation = new AssetBundleLoadAssetOperationSimulation(target, null); //return operation.GetAsset<T>(); } else #endif { AssetBundle bundle = LoadOrGetAssetsBundle(gas); if (bundle != null) { T go = bundle.LoadAsset <T>(gas.AssetName); if (go == null) { Debug.LogError("go == null gas.BundleFullName:" + gas.BundleFullName + " gas.AssetName:" + gas.AssetName); } return(go); } //CommonData.tempLog += "bundle == null: "; //CommonData.tempLog += gas.AssetName; //CommonData.tempLog += gas.BundleFullName; return(null); } }
// Load level from the given assetBundle. public AssetBundleLoadOperation LoadLevelAsync(GameAssetStruct gas, bool isAdditive) { Log(LogType.Info, "Loading " + gas.AssetName + " from " + gas.BundleFullName + " bundle"); AssetBundleLoadOperation operation = null; #if UNITY_EDITOR if (SimulateAssetBundleInEditor) { operation = new AssetBundleLoadLevelSimulationOperation(gas.BundleFullName, gas.AssetName, isAdditive, null); } else #endif { LoadAssetBundleAsy(gas.BundleFullName); operation = new AssetBundleLoadLevelOperation(gas.BundleFullName, gas.AssetName, isAdditive, null); m_InProgressOperations.Add(operation); } return(operation); }
/// <summary> /// 初始化bundle路径和Manifest /// </summary> /// <param name="bundleDirectory"></param> /// <param name="pCallBack"></param> /// <returns></returns> public void Initialize() { if (hasInitialize) { return; } string[] list = null; hasInitialize = true; this.SetSourceAssetBundleDirectory(); string filePath2BundleListPath = null; #if UNITY_EDITOR // If we're in Editor simulation mode, we don't need the manifest assetBundle. if (SimulateAssetBundleInEditor) { filePath2BundleListPath = Application.dataPath + "/" + AppConst.ResRoot + AppConst.FilePath2BundleListName; list = File.ReadAllLines(filePath2BundleListPath); GameAssetStruct.Init(list); return; } #endif filePath2BundleListPath = GetBundleFullUrl(AppConst.FilePath2BundleListName); list = File.ReadAllLines(filePath2BundleListPath); GameAssetStruct.Init(list); string bundleName = AppConst.BundlesFloderName; string path = GetBundleFullUrl(bundleName); AssetBundle bundle = AssetBundle.LoadFromFile(path); m_AssetBundleManifest = bundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); if (bundle != null) { AddResourceLoaded(bundleName, bundle); } }
// Load asset from the given assetBundle. public AssetBundleLoadAssetOperation LoadAssetAsync <T>(string assetPath, System.Action <T> pCallBack) where T : UnityEngine.Object { GameAssetStruct gas = new GameAssetStruct(assetPath); return(LoadAssetAsync(gas, pCallBack)); }
/// <summary> /// 同步加载某种类型资源 /// </summary> public T LoadAsset <T>(string assetPath) where T : UnityEngine.Object { GameAssetStruct gas = new GameAssetStruct(assetPath); return(LoadAsset <T>(gas)); }