/// <summary> /// Unloads the bundle with the given name. /// </summary> /// <param name="bundleName">Name of the bundle to unload without variant extension</param> private void UnloadBundle(string bundleName) { AssetBundleRecord record = this.GetLoadedBundleRecordByName(bundleName); if (null == record) { return; } record.bundle.Unload(true); m_loadedAssetBundles.Remove(bundleName); }
/// <summary> /// Returns the bundle at the specified path, loading it if necessary. /// Unloads previously loaded bundles if necessary when dealing with variants. /// </summary> /// <returns>Returns the loaded bundle, null if it could not be loaded.</returns> /// <param name="path">Path of bundle to get</param> private AssetBundle LoadBundle(string path) { if (string.IsNullOrEmpty(path)) { return(null); } string extension = Path.GetExtension(path); string bundleName = path.Substring(0, path.Length - extension.Length); // Check if we have a record for this bundle AssetBundleRecord record = GetLoadedBundleRecordByName(bundleName); AssetBundle bundle = null; if (null != record) { // Unload existing bundle if variant names differ, otherwise use existing bundle if (!record.path.Equals(path)) { UnloadBundle(bundleName); } else { bundle = record.assetBundle; } } if (null == bundle) { // Load the bundle bundle = AssetBundle.LoadFromFile(path); if (null == bundle) { return(null); } m_LoadedAssetBundles[bundleName] = new AssetBundleRecord(path, bundle); // Load the bundle's assets string[] assetNames = bundle.GetAllAssetNames(); foreach (string name in assetNames) { bundle.LoadAsset(name); } } return(bundle); }
/// <summary> /// Returns the bundle at the specified path, loading it if necessary. /// Unloads previously loaded bundles if necessary when dealing with variants. /// </summary> /// <returns>Returns the loaded bundle, null if it could not be loaded.</returns> /// <param name="varPath">Path of bundle to get</param> private AssetBundle LoadBundle(string varPath) { if (string.IsNullOrEmpty(varPath)) { return(null); } var bundleName = Path.HasExtension(varPath) ? Path.GetFileNameWithoutExtension(varPath) : Path.GetFileName(varPath); // Check if we have a record for this bundle AssetBundleRecord record = GetLoadedBundleRecordByName(bundleName); AssetBundle bundle = null; if (null != record) { // Unload existing bundle if variant names differ, otherwise use existing bundle if (!record.path.Equals(varPath)) { UnloadBundle(bundleName); } else { bundle = record.bundle; } } if (null == bundle) { // Load the bundle bundle = AssetBundle.LoadFromFile(varPath); if (null == bundle) { return(null); } m_loadedAssetBundles[bundleName] = new AssetBundleRecord(varPath, bundle); // Load the bundle's assets string[] assetNames = bundle.GetAllAssetNames(); foreach (string name in assetNames) { bundle.LoadAsset(name); } } return(bundle); }
/// <summary> /// Returns the bundle at the specified path, loading it if necessary. /// Unloads previously loaded bundles if necessary when dealing with variants. /// </summary> /// <returns>Returns the loaded bundle, null if it could not be loaded.</returns> /// <param name="path">Path of bundle to get</param> private AssetBundle LoadBundle(string path) { if (string.IsNullOrEmpty(path)) { return(null); } string extension = Path.GetExtension(path); string bundleName = path.Substring(0, path.Length - extension.Length); // Check if we have a record for this bundle AssetBundleRecord record = GetLoadedBundleRecordByName(bundleName); AssetBundle bundle = null; if (null != record) { // Unload existing bundle if variant names differ, otherwise use existing bundle if (!record.path.Equals(path)) { UnloadBundle(bundleName); } else { bundle = record.bundle; } } if (null == bundle) { // Load the bundle if (m_IsEncrypt) { byte[] buffer; using (var fs = new FileStream(path, FileMode.Open)) { buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); } try { bundle = AssetBundle.LoadFromMemory(Crypto.AesDecryptBytes(buffer)); } catch (Exception e) { Debug.LogError(e.Message); bundle = null; } } else { bundle = AssetBundle.LoadFromFile(path); } if (null == bundle) { return(null); } m_loadedAssetBundles[bundleName] = new AssetBundleRecord(path, bundle); // Load the bundle's assets string[] assetNames = bundle.GetAllAssetNames(); foreach (string name in assetNames) { bundle.LoadAsset(name); } } return(bundle); }