Пример #1
0
        private IEnumerator LoadAssetBundleFromFile(string assetBundleName)
        {
            if (m_loadedAssetBundles.ContainsKey(assetBundleName))
            {
                yield break;
            }

            string url = m_loadFromFileURL + assetBundleName;

            AssetBundleCreateRequest assetBundleCreateRequest = UnityEngine.AssetBundle.LoadFromFileAsync(url);

            yield return(assetBundleCreateRequest);

            if (assetBundleCreateRequest.assetBundle == null)
            {
                JSLDebug.LogErrorFormat("[AssetBundleManager] - Failed to load AssetBundle from {0}", url);
                yield break;
            }

#if UNITY_EDITOR
            ReassignShader(assetBundleCreateRequest.assetBundle);
#endif

            var loadedAssetBundle = new LoadedAssetBundle(assetBundleCreateRequest.assetBundle);
            m_loadedAssetBundles[assetBundleName] = loadedAssetBundle;
        }
Пример #2
0
        private IEnumerator LoadCatalogFromNetwork()
        {
            JSLDebug.LogFormat("[AssetBundleManager] - Start download AssetBundleCatalog at frame {0}", Time.frameCount);

            UnityWebRequest request = UnityWebRequest.Get(m_downloadingURL + AssetBundleDef.CATALOG_FILE_NAME);

            request.SendWebRequest();

            while (!request.isDone)
            {
                yield return(null);
            }

            if (!string.IsNullOrEmpty(request.error))
            {
                m_assetBundleCatalogs = null;
                JSLDebug.LogErrorFormat("[AssetBundleManager] - Download AssetBundleCatalog failed. Error log \"{0}\"", request.error);
            }
            else
            {
                m_assetBundleCatalogs = new AssetBundleCatalogs(request.downloadHandler.text);
                JSLDebug.LogFormat("[AssetBundleManager] - Download AssetBundleCatalog complete at frame {0}", Time.frameCount);
            }

            request.Dispose();
        }
Пример #3
0
        public override bool IsDone()
        {
            if (null == m_request && null != m_downloadingError)
            {
                JSLDebug.LogErrorFormat("[AssetBundleLoadAssetRequestFull] - Load AssetBundle '{0}' failed with reason {1}", m_assetBundleName, m_downloadingError);
                return(true);
            }

            return(null != m_request && m_request.isDone);
        }
Пример #4
0
        private IEnumerator StartLoadAsync <T>(string assetBundleName, string assetName, Action <T, object> callback, object customData, bool unloadAutomatically) where T : UnityEngine.Object
        {
            if (string.IsNullOrEmpty(assetName))
            {
                JSLDebug.LogErrorFormat(LOG_LOAD_ASSET_FAILED, assetName, assetBundleName);
                callback?.Invoke(null, customData);
                yield break;
            }

            string cacheKey = GetCacheKey <T>(assetBundleName, assetName);

            if (IsPrefab <T>())
            {
                yield return(PreloadAsync <GameObject>(cacheKey, assetBundleName, assetName));
            }
            else
            {
                yield return(PreloadAsync <T>(cacheKey, assetBundleName, assetName));
            }

            if (!InCache(cacheKey))
            {
                JSLDebug.LogErrorFormat(LOG_LOAD_ASSET_FAILED, assetName, assetBundleName);

                callback?.Invoke(null, customData);
                RemoveAsyncLoadingReferencedCounts(cacheKey);

                yield break;
            }

            LoadedResource res = m_loadedResources[cacheKey];

            res.referencedCount++;

            T asset = res.resource as T;

            if (asset == null)
            {
                asset = (res.resource as GameObject).GetComponent <T>();
            }

            callback?.Invoke(asset, customData);
            RemoveAsyncLoadingReferencedCounts(cacheKey);

            if (unloadAutomatically)
            {
                Unload <T>(assetBundleName, assetName);
            }
        }
Пример #5
0
        public static void Build(string outputPath)
        {
            string[] allAssetBundleNames = AssetDatabase.GetAllAssetBundleNames();

            string   fullPath;
            Hash128  hash128;
            uint     crc = 0;
            FileInfo fileInfo;

            string catalogContents = string.Empty;

            for (int i = 0; i < allAssetBundleNames.Length; i++)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(allAssetBundleNames[i]);
                if (assetPaths.Length == 0)
                {
                    JSLDebug.LogErrorFormat("[AssetBundleCatalogBuilder] - The AssetBundle '{0}' is empty. It might be a folder, need to check it again!", allAssetBundleNames[i]);
                    continue;
                }

                fullPath = outputPath + "/" + allAssetBundleNames[i];

                if (!BuildPipeline.GetCRCForAssetBundle(fullPath, out crc))
                {
                    JSLDebug.LogErrorFormat("[AssetBundleCatalogBuilder] - Failed to get CRC from {0}", fullPath);
                    continue;
                }

                if (!BuildPipeline.GetHashForAssetBundle(fullPath, out hash128))
                {
                    JSLDebug.LogErrorFormat("[AssetBundleCatalogBuilder] - Failed to get Hash128 from {0}", fullPath);
                    continue;
                }

                fileInfo = new FileInfo(fullPath);
                if (!fileInfo.Exists)
                {
                    JSLDebug.LogErrorFormat("[AssetBundleCatalogBuilder] - Failed to get file size from {0}", fullPath);
                    continue;
                }

                if (i != 0)
                {
                    catalogContents += System.Environment.NewLine;
                }

                catalogContents += allAssetBundleNames[i];
                catalogContents += System.Environment.NewLine;
                catalogContents += System.Convert.ToString(hash128);
                catalogContents += System.Environment.NewLine;
                catalogContents += crc.ToString();
                catalogContents += System.Environment.NewLine;
                catalogContents += GetFileSizeInB(fileInfo.Length).ToString();
            }

            string catalogPath = Path.Combine(outputPath, AssetBundleDef.CATALOG_FILE_NAME);

            File.WriteAllText(catalogPath, catalogContents);

            JSLDebug.LogFormat("[AssetBundleCatalogBuilder] - Build catalog.txt success in {0}", catalogPath);
        }