Example #1
0
 public int Init(ref AssetBundleContext context)
 {
     try
     {
         string perpath = AssetBundleHelper.GetBundlePersistentPath(AssetBundleConfig.DepFileName);
         if (!AddPersistentAtPath(ref context, perpath))
         {
             string streampath = AssetBundleHelper.GetBundleStreamPath(AssetBundleConfig.DepFileName);
             if (!AddStreamAtPath(ref context, streampath))
             {
                 Debug.Log("BundleStreamPath dont have depfile");
                 return(-1);
             }
         }
         else if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
         {
             Debug.Log("PersistentPath have depfile");
         }
         return(0);
     }
     catch (Exception e)
     {
         Debug.LogException(e);
         return(-1);
     }
 }
        public IEnumerator Init(string url)
        {
            using (var request = UnityWebRequest.Get(url + "/" + AssetBundleConfig.DepFileName))
            {
                yield return(request.Send());

                if (request.responseCode != 200)
                {
                    Debug.LogErrorFormat("Http Error :{0} Info: {1}", request.url, request.downloadHandler.text);
                }
                else if (!string.IsNullOrEmpty(request.error))
                {
                    Debug.LogErrorFormat("Error :{0} Info: {1}", request.error, request.downloadHandler.text);
                }
                else
                {
                    string perpath = AssetBundleHelper.GetBundlePersistentPath(AssetBundleConfig.DepFileName);
                    string dirname = Path.GetDirectoryName(perpath);
                    if (!Directory.Exists(dirname))
                    {
                        Directory.CreateDirectory(dirname);
                    }

                    File.WriteAllBytes(perpath, request.downloadHandler.data);

                    var getter = AssetBundleTypeGetter.GetDepReader();

                    List <AssetBundleInfo> list = new List <AssetBundleInfo>();
                    getter.ReadBytes(list, request.downloadHandler.data);

                    if (_dict == null)
                    {
                        _dict = new Dictionary <IgnoreCaseString, string>();
                    }

                    foreach (var element in list)
                    {
                        IgnoreCaseString key = new IgnoreCaseString(element.AssetBundleName);
                        _dict[key] = url + "/" + element.AssetBundleName;
                    }
                }

                IsDone = true;
            }
        }
        protected virtual void Load(ref AssetBundleContext context, ref AssetBundleTask task,
                                    ref GameAssetBundle gameAssetBundle, ref bool retcode, bool mainloadrequest = false)
        {
            if (gameAssetBundle.IsException())
            {
                return;
            }

            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
            {
                Debug.LogFormat("Load --- {0}", task);
            }

            gameAssetBundle.AssetStatus |= AssetBundleStatus.Loading;
            //not loaded
            if (gameAssetBundle.AssetBundle == null && gameAssetBundle.LoadRequest == null && gameAssetBundle.LoadAssetRequest == null)
            {
                if (IsEnableLoadAssetBunlde(ref context, ref task, ref gameAssetBundle))
                {
                    string assetfilename = AssetBundleConfig.Convert(task.AssetBundleName);
                    string path          = AssetBundleHelper.GetBundlePersistentPath(assetfilename);
                    bool   fileExist     = File.Exists(path);

                    string url;
                    bool   indownloadcache = context.Cache.TryGetDownloadUrl(new IgnoreCaseString(task.AssetBundleName), out url);
                    bool   canload         = false;
                    if (indownloadcache && !fileExist)
                    {
                        AssetDownloadInfo downloadInfo = new AssetDownloadInfo();
                        downloadInfo.AssetPath       = task.AssetPath;
                        downloadInfo.AssetBundleName = task.AssetBundleName;
                        if (!context.DownLoadQueue.Contains(downloadInfo))
                        {
                            downloadInfo.DstPath = path;
                            downloadInfo.Url     = url;
                            downloadInfo.TaskId  = task.TaskId;
                            context.DownLoadQueue.Add(downloadInfo);
                        }
                    }
                    else if (fileExist)
                    {
                        canload = true;
                    }
                    else if (!fileExist)
                    {
                        string streampath = AssetBundleHelper.GetBundleStreamPath(assetfilename);
                        if (File.Exists(streampath))
                        {
                            canload = true;
                            path    = streampath;
                        }
                    }

                    if (canload)
                    {
                        if (task.IsAsync())
                        {
                            gameAssetBundle.LoadRequest = AssetBundle.LoadFromFileAsync(path);
                            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                            {
                                Debug.LogFormat("LoadFromFileAsync AssetBundle :{0} ", task.AssetBundleName);
                            }
                        }
                        else
                        {
                            gameAssetBundle.AssetBundle = AssetBundle.LoadFromFile(path);
                            if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                            {
                                Debug.LogFormat("LoadFromFile AssetBundle :{0} ", task.AssetBundleName);
                            }

                            AfterLoadAssetBundle(ref context, ref task, ref gameAssetBundle, ref retcode, ref mainloadrequest);
                        }
                    }
                    else if (!indownloadcache)
                    {
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.FileNotExist;
                        AddException(ref context, task.AssetPath);
                        Debug.LogError(string.Format("cant load :{0}", task));
                    }
                }
            }
            else if (gameAssetBundle.LoadRequest != null)// in asyncing Load AssetBundle
            {
                if (gameAssetBundle.LoadRequest.isDone)
                {
                    if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                    {
                        Debug.LogFormat("Async Task for Load AssetBundle is Done :{0} ", task);
                    }
                    gameAssetBundle.AssetBundle = gameAssetBundle.LoadRequest.assetBundle;

                    AfterLoadAssetBundle(ref context, ref task, ref gameAssetBundle, ref retcode, ref mainloadrequest);
                    gameAssetBundle.LoadRequest = null;
                }
            }
            else if (gameAssetBundle.LoadAssetRequest != null && mainloadrequest)// Load Asset
            {
                if (gameAssetBundle.LoadAssetRequest.isDone)
                {
                    var loadfinishasset = gameAssetBundle.LoadAssetRequest.asset;

                    if (loadfinishasset != null)
                    {
                        if (AssetBundleConfig.DebugMode.HasEnum(DebugMode.Detail))
                        {
                            Debug.LogFormat("Async Task Add Asset :{0} ", task);
                        }
                        gameAssetBundle.AddAsset(task.AssetPath, gameAssetBundle.LoadAssetRequest.asset);
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.InMemory;
                        if (task.Result.ProgresCallback != null)
                        {
                            ProgressArgs progressArgs = new ProgressArgs(1, 0, task.AssetPath, true);
                            task.Result.ProgresCallback(ref progressArgs);
                        }
                        task.FinishTime = Time.realtimeSinceStartup;
                        retcode         = true;
                    }
                    else
                    {
                        Debug.LogErrorFormat("{0} has 0 Assets", gameAssetBundle.AssetBundle);
                        gameAssetBundle.AssetStatus |= AssetBundleStatus.NotInMemory;
                    }

                    gameAssetBundle.LoadAssetRequest = null;
                }
            }
            else
            {
                if (mainloadrequest)
                {
                    LoadMainAsset(ref context, ref task, ref gameAssetBundle, ref retcode);
                }
            }
        }