/// <summary>
    /// Load the requested AssetBundle synchronously. Calling this method will freeze everything else as it is executed on
    /// the main thread. If you want your bundle to be loaded in the background, please use LoadABAsync (coroutine) or
    /// LoadAbSyncLocally
    /// </summary>
    /// <param name="ab_name"> Asset Bundle name to be loaded ( it should be include in the assetbundles folder) </param>
    /// <param name="persist"> Does this AssetBundle persist accross scenes? </param>
    /// <returns> True if the AssetBundle has been loaded correctly </returns>
    public bool LoadAB(string ab_name, bool persist = false)
    {
        try {
            // If the AssetBundles list already contains the requested AssetBundle, just return true
            if (dictAssetBundleRefs.ContainsKey(ab_name))
            {
                return(true);
            }

            // Load the AssetBundle from the StreamingAssets + platform folder
            AssetBundle myLoadedAssetBundle = AssetBundle.LoadFromFile(
                Path.Combine(Application.streamingAssetsPath, ab_path + ab_name));

            if (myLoadedAssetBundle == null)
            {
                Debug.Log("Failed to load AssetBundle!");
                return(false);
            }

            // If everything went fine, include it in our AssetBundles list
            AssetBundleRef abRef = new AssetBundleRef()
            {
                assetBundle   = myLoadedAssetBundle,
                persistAcross = persist
            };

            dictAssetBundleRefs.Add(ab_name, abRef);
            return(true);
        } catch (Exception e) {
            Debug.Log("Error while loasing synch AssetBundle: " + ab_name + ", error: " + e.Message);
            return(false);
        }
    }
    /// <summary>
    /// Coroutine to load the requested assetbundle asynchronously
    /// </summary>
    /// <param name="ab_name"> Asset Bundle name to be loaded ( it should be include in the assetbundles folder) </param>
    /// <param name="async_callback"> Callback once the load process is finished (if not null)</param>
    /// <param name="persist"> Does this AssetBundle persist accross scenes? </param>
    /// <returns> Coroutine </returns>
    public IEnumerator LoadABAsync(string ab_name, Action async_callback, bool persist = false)
    {
        if (!dictAssetBundleRefs.ContainsKey(ab_name))         // Is it loaded already?
        {
            var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, ab_path + ab_name));
            yield
            return(bundleLoadRequest);

            var myLoadedAssetBundle = bundleLoadRequest.assetBundle;

            if (myLoadedAssetBundle == null)
            {
                Debug.Log("Failed to load AssetBundle!");
                yield
                return(null);
            }

            // If everything went fine, include it in our AssetBundles list
            AssetBundleRef abRef = new AssetBundleRef()
            {
                assetBundle   = myLoadedAssetBundle,
                persistAcross = persist
            };

            dictAssetBundleRefs.Add(ab_name, abRef);
        }

        if (async_callback != null)
        {
            async_callback();
        }
    }
Example #3
0
    // Download an AssetBundle
    public static IEnumerator downloadAssetBundle(string url, int version)
    {
        string keyName = url + version.ToString();

        if (dictAssetBundleRefs.ContainsKey(keyName))
        {
            yield return(null);
        }
        else
        {
            while (!Caching.ready)
            {
                yield return(null);
            }

            using (WWW www = WWW.LoadFromCacheOrDownload(url, version)){
                yield return(www);

                if (www.error != null)
                {
                    throw new Exception("WWW download:" + www.error);
                }
                AssetBundleRef abRef = new AssetBundleRef(url, version);
                abRef.assetBundle = www.assetBundle;
                dictAssetBundleRefs.Add(keyName, abRef);
            }
        }
    }
    public static void AddAsset(string keyName, string url, int version, AssetBundle assetBundle)
    {
        AssetBundleRef abRef = new AssetBundleRef(url, version);

        abRef.assetBundle = assetBundle;
        dictAssetBundleRefs.Add(keyName, abRef);
    }
	public static IEnumerator DownloadAssetBundle (string url, int version)
	{
		string assetPath = GlobalParams.Instance.AssetBundlePath;
		string keyName = assetPath + url + version.ToString ();
		
		if (dictAssetBundleRefs.ContainsKey (keyName))
		{
			yield return null;
		}
		else 
		{
			Debug.Log(assetPath + url);
			WWW assetBundle = WWW.LoadFromCacheOrDownload (assetPath + url, version);
			
			yield return assetBundle;
			if (assetBundle.error != null)
			{
				Debug.Log(assetBundle.error);
				throw new Exception ("WWW download:" + assetBundle.error);
			}
			else
			{
				Debug.Log("DOWNLOADED ASSETBUNDLE COMPLETE ====> " + assetBundle.assetBundle.mainAsset.name);
				AssetBundleRef abRef = new AssetBundleRef (assetPath + url, version);
				abRef.assetBundle = assetBundle.assetBundle;
				dictAssetBundleRefs.Add (keyName, abRef);
			}
		}
	}
Example #6
0
 public static void CacheSpriteAsync(AssetBundleRef abr, Action callback)
 {
     abr.LoadAllAssetsAsync((assets) =>
     {
         CacheSprite(abr, assets);
         callback();
     });
 }
        public static void LoadAssetBundle(string itemId, Hash128 version, Action <bool> callback)
        {
            Debug.Log("Load Asset: " + itemId);
            AssetBundleRef skinRef = new AssetBundleRef(TransferItemIdToBundleName(itemId), version);

            AssetBundleManager.DownloadAssetBundle(skinRef, callback);
            LastestLoadedSkinBundleName = skinRef.name;
        }
Example #8
0
 private static void addRef(string keyName, bool isFixed, AssetBundleRef abRef)
 {
     _refs.Add(keyName, abRef);
     if (!isFixed)
     {
         _refslist.Add(abRef);
     }
 }
 public static AssetBundle getAssetBundle (string name, int version){
     string url = Application.persistentDataPath + "/" + name;
     if (!dictAssetBundleRefs.ContainsKey(url)) {
         AssetBundleRef abref = new AssetBundleRef(url, version);
         abref.assetBundle = AssetBundle.CreateFromFile(url);
         dictAssetBundleRefs[url] = abref;
     }
     return dictAssetBundleRefs[url].assetBundle;
 }
Example #10
0
    public static AssetManagerRequest LoadAsync <T>(String name) where T : UnityEngine.Object
    {
        //if (!name.EndsWith(".mes"))
        //{
        //    Log.Message(typeof(T).Name + ": " + name);
        //    Log.Message(Environment.StackTrace);
        //}

        if (AssetManagerForObb.IsUseOBB)
        {
            return(AssetManagerForObb.LoadAsync <T>(name));
        }

        if (!UseBundles || AssetManagerUtil.IsEmbededAssets(name))
        {
            ResourceRequest resReq = Resources.LoadAsync <T>(name);
            if (resReq != null)
            {
                return(new AssetManagerRequest(resReq, null));
            }
        }

        String  belongingBundleFilename = AssetManagerUtil.GetBelongingBundleFilename(name);
        Boolean flag1 = AssetManagerUtil.CheckModuleBundleFromName(AssetManagerUtil.ModuleBundle.CommonAssets, name);

        if (!String.IsNullOrEmpty(belongingBundleFilename) && DictAssetBundleRefs.ContainsKey(belongingBundleFilename))
        {
            AssetBundleRef assetBundleRef = DictAssetBundleRefs[belongingBundleFilename];
            if (assetBundleRef.assetBundle != null)
            {
                String name1 = AssetManagerUtil.GetResourcesBasePath() + name + AssetManagerUtil.GetAssetExtension <T>(name);
                //Boolean flag2 = name.IndexOf("atlas_a", StringComparison.Ordinal) != -1;
                AssetBundleRequest assReq = assetBundleRef.assetBundle.LoadAssetAsync(name1);
                if (assReq != null)
                {
                    return(new AssetManagerRequest(null, assReq));
                }

                if (!flag1 && ForceUseBundles)
                {
                    //if (Application.platform != RuntimePlatform.Android && flag2)
                    //    return null;

                    return(null);
                }
            }
        }

        ResourceRequest resReq1 = Resources.LoadAsync <T>(name);

        if (resReq1 != null)
        {
            return(new AssetManagerRequest(resReq1, null));
        }

        return(null);
    }
Example #11
0
    public void DestoryAssetBundle(string path)
    {
        AssetBundleRef ref2 = null;

        if (this.assetBundleMaps_.TryGetValue(path, out ref2) && ref2.Release())
        {
            this.assetBundleMaps_.Remove(path);
        }
    }
Example #12
0
 public static bool AddAssetBundle(string abname, AssetBundleRef abRef)
 {
     if (dictAssetBundleRefs.ContainsKey(abname))
     {
         Debug.Log("重复添加assetbundle,name:" + abname);
         return(false);
     }
     dictAssetBundleRefs.Add(abname, abRef);
     return(true);
 }
Example #13
0
    public static T Load <T>(String name, Boolean suppressError = false) where T : UnityEngine.Object
    {
        //if (!name.EndsWith(".mes"))
        //{
        //    Log.Message(typeof(T).Name + ": " + name);
        //    Log.Message(Environment.StackTrace);
        //}

        if (AssetManagerForObb.IsUseOBB)
        {
            return(AssetManagerForObb.Load <T>(name, suppressError));
        }

        if (!UseBundles || AssetManagerUtil.IsEmbededAssets(name))
        {
            return(Resources.Load <T>(name));
        }

        String  belongingBundleFilename = AssetManagerUtil.GetBelongingBundleFilename(name);
        Boolean flag1 = AssetManagerUtil.CheckModuleBundleFromName(AssetManagerUtil.ModuleBundle.CommonAssets, name);

        if (!String.IsNullOrEmpty(belongingBundleFilename) && DictAssetBundleRefs.ContainsKey(belongingBundleFilename))
        {
            AssetBundleRef assetBundleRef = DictAssetBundleRefs[belongingBundleFilename];
            if (assetBundleRef.assetBundle != null)
            {
                String  name1 = AssetManagerUtil.GetResourcesBasePath() + name + AssetManagerUtil.GetAssetExtension <T>(name);
                Boolean flag2 = name.IndexOf("atlas_a", StringComparison.Ordinal) != -1;
                T       obj   = assetBundleRef.assetBundle.LoadAsset <T>(name1);
                if (obj != null)
                {
                    return(obj);
                }

                if (Application.platform != RuntimePlatform.Android && flag2)
                {
                    return(null);
                }

                if (!flag1 && ForceUseBundles)
                {
                    return(null);
                }
            }
        }

        if (ForceUseBundles)
        {
            return(null);
        }

        T obj1 = Resources.Load <T>(name);

        return(obj1);
    }
Example #14
0
    public static void AddAssetBundle(AssetBundle bundle, string url, string name, GameObject assetGO)
    {
        AssetBundleRef abRef = new AssetBundleRef(url, 1);

        abRef.assetBundle = bundle;
        abRef.assetGO     = assetGO;
        if (!dictAssetBundleRefs.ContainsKey(name))
        {
            dictAssetBundleRefs.Add(name, abRef);
        }
    }
Example #15
0
 public static void CheckShouldCacheSpriteAsync(AssetBundleRef abr, Action callback)
 {
     if (abr.Name.Contains("atlas"))
     {
         SpriteManager.CacheSpriteAsync(abr, callback);
     }
     else
     {
         callback();
     }
 }
    public static IEnumerator RetryDownload(string link)
    {
        string keyName       = link + "1";
        bool   InternetError = false;

        AssetBundleManager.Unload(link, 1, false);

        using (WWW www = WWW.LoadFromCacheOrDownload(link, 1)) {
            while (!www.isDone)
            {
                yield return(ConnectionController.Instance.StartCoroutine(ConnectionController.Instance.IeCheckServices()));

                if (!ConnectionController.Instance._checking && (!ConnectionController.Instance._internetOutput || !ConnectionController.Instance._serverOutput))
                {
                    InternetError = true;
                    break;
                }
                else if (!ConnectionController.Instance._checking)
                {
                    yield return(new WaitForSeconds(1f));
                }
                else if (ConnectionController.Instance._internetOutput || ConnectionController.Instance._serverOutput)
                {
                    ConnectionController.Instance.InternetPopup.SetActive(false);
                }
                yield return(null);
            }
            if (www.error != null || InternetError)
            {
                ///will remove the old file from cashe which cause the problem
                AssetBundleManager.Unload(link, 1, false);
                //				DownloadContent.Instance.reporttext.text = "Download error please retry " + www.error;
                Debug.Log("Error : " + www.error);
                DownloadContent.Instance.StopAllCoroutines();
                DownloadContent.Instance.IeCheckForDownloadList();
            }
            else
            {
                AssetBundleRef abRef = new AssetBundleRef(link, 1);
                abRef.assetBundle = www.assetBundle;

                if (!dictAssetBundleRefs.ContainsKey(keyName))
                {
                    dictAssetBundleRefs.Add(keyName, abRef);
                }
                else
                {
                    //				DownloadContent.Instance.reporttext.text = "Asset is unloading plz wait";
                    Debug.Log("This is Just Test that how we can unload asset which is in cache");
                    AssetBundleManager.Unload(link, 1, false);
                }
            }
        }
    }
    public static AssetBundle getAssetBundle(string name, int version)
    {
        string url = Application.persistentDataPath + "/" + name;

        if (!dictAssetBundleRefs.ContainsKey(url))
        {
            AssetBundleRef abref = new AssetBundleRef(url, version);
            abref.assetBundle        = AssetBundle.CreateFromFile(url);
            dictAssetBundleRefs[url] = abref;
        }
        return(dictAssetBundleRefs[url].assetBundle);
    }
Example #18
0
 public static void AddBundle(string url, int version, AssetBundle bundle)
 {
     var keyName = url + version;
     if (dictAssetBundleRefs.ContainsKey(keyName))
     {
         Debug.Log(keyName + " is already loaded");
         return;
     }
     var abRef = new AssetBundleRef(url, version) {assetBundle = bundle};
     dictAssetBundleRefs.Add(keyName, abRef);
     Debug.Log(keyName + " added");
 }
Example #19
0
	public static void addAssetBundle(string url, int version, AssetBundle assetbundle )
	{
		Debug.Log("addAssetBundle url = "+ url);
		if( getAssetBundle(url, version ) == null )
		{
			Debug.Log("addAssetBundle Success");
			string keyName = url + version.ToString();
			AssetBundleRef abRef = new AssetBundleRef (url, version);
			abRef.assetBundle = assetbundle;
			dictAssetBundleRefs.Add (keyName, abRef);
		}
	}
Example #20
0
 public static AssetBundle LoadAssetBundleWithFile(string file)
 {
     AssetBundle bundle = getAssetBundle(file, 1);
     if (bundle == null) {
         string keyName = file + 1;
         bundle = AssetBundle.CreateFromFile(file);
         AssetBundleRef abRef = new AssetBundleRef (file, 1);
         abRef.assetBundle = bundle;
         dictAssetBundleRefs.Add (keyName, abRef);
     }
     return bundle;
 }
    // Download an AssetBundle
    public static IEnumerator downloadAssetBundle(string url, int version)
    {
        string keyName       = url + version.ToString();
        bool   InternetError = false;

        AssetBundleManager.Unload(url, version, false);

//		float time = 0;

        using (WWW www = WWW.LoadFromCacheOrDownload(url, version)) {
            while (!www.isDone)
            {
                CoroutineWithData cd = new CoroutineWithData(ConnectionController.Instance, ConnectionController.Instance.IeCheckServices());
                yield return(cd.coroutine);

                if (cd.result.ToString() == "True" || cd.result.ToString() == "true")
                {
                    ConnectionController.Instance.InternetPopup.SetActive(false);
                }
                else
                {
//					InternetError = true;
                    yield return(null);
                }
                yield return(null);
            }
            if (www.error != null || InternetError)
            {
                ///will remove the old file from cashe which cause the problem
                AssetBundleManager.Unload(url, version, false);
//				DownloadContent.Instance.reporttext.text = "Download error please retry " + www.error;
                Debug.Log("Error : " + www.error);
                DownloadContent.Instance.StartCoroutines();
            }
            else
            {
                AssetBundleRef abRef = new AssetBundleRef(url, version);
                abRef.assetBundle = www.assetBundle;

                if (!dictAssetBundleRefs.ContainsKey(keyName))
                {
                    dictAssetBundleRefs.Add(keyName, abRef);
                }
                else
                {
                    //				DownloadContent.Instance.reporttext.text = "Asset is unloading plz wait";
                    Debug.Log("This is Just Test that how we can unload asset which is in cache");
                    AssetBundleManager.Unload(url, version, false);
                }
            }
        }
    }
Example #22
0
        private static IEnumerator loadAssetBundleCoroutine(string path, int version, bool isFixed, LoadedCallback callback)
        {
            string         keyName = makeKey(path, version);
            AssetBundleRef abRef;

            if (_refs.TryGetValue(keyName, out abRef))
            {
                refreshRef(abRef);
                if (abRef.assetBundle != null)
                {
                    callback(abRef.assetBundle);
                    yield return(null);
                }
                else
                {   // in www loading ...
                    abRef.callbacks.Add(callback);
                }
            }
            else
            {
                abRef = new AssetBundleRef(null, path, version);
                abRef.callbacks.Add(callback);
                addRef(keyName, isFixed, abRef);

                using (WWW www = WWW.LoadFromCacheOrDownload(path, version))
                {
                    yield return(www);

                    if (www.error != null)
                    {
                        _refs.Remove(keyName);
                        throw new Exception("WWW download:" + www.error + "! [path]" + path);
                    }

                    abRef = null;
                    if (_refs.TryGetValue(keyName, out abRef))
                    {
                        abRef.assetBundle = www.assetBundle;
                        for (int i = 0, n = abRef.callbacks.Count; i < n; i++)
                        {
                            abRef.callbacks[i](abRef.assetBundle);
                        }
                        abRef.callbacks.Clear();
                    }
                    else
                    {
                        www.assetBundle.Unload(true);
                        throw new Exception("loadAssetBundleCoroutine: can not find keyName:" + keyName);
                    }
                }
            }
        }
Example #23
0
    public static AssetBundle LoadAssetBundleWithFile(string file)
    {
        AssetBundle bundle = getAssetBundle(file, 1);

        if (bundle == null)
        {
            string keyName = file + 1;
            bundle = AssetBundle.CreateFromFile(file);
            AssetBundleRef abRef = new AssetBundleRef(file, 1);
            abRef.assetBundle = bundle;
            dictAssetBundleRefs.Add(keyName, abRef);
        }
        return(bundle);
    }
Example #24
0
    /// <summary>
    /// 同步加载assetbundle
    /// </summary>
    /// <param name="path">本地加载路径</param>
    /// <param name="abname">文件名</param>
    /// <returns></returns>
    public static bool LoadAssetBundleFromLocal(string path, string abname)
    {
#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        if (!Luancher.UpdateWithLuncher)
        {
#if UNITY_IOS
            path = Application.dataPath + "/AssetBundles/IOS/";
#elif UNITY_ANDROID
            path = Application.dataPath + "/AssetBundles/Android/";
#else
            path = Application.dataPath + "/AssetBundles/Windows/";
#endif
        }
#endif

        if (dictAssetBundleRefs.ContainsKey(abname))
        {
            //Debug.Log("重复加载assetbundle,name:" + abname);
            return(true);
        }
        if (!File.Exists(path + abname))
        {
            //Debug.Log("加载的Assetbundle文件不存在" + abname);
            return(false);
        }

        //加载依赖资源
        LoadDependenciesAssetBundle(abname);

        //Debug.Log("加载bundle名称:" + abname+" 开始:" + DateTime.Now.Ticks);
        AssetBundle bundle = AssetBundle.LoadFromFile(path + abname);
        if (bundle == null)
        {
            return(false);
        }

        //AssetBundleCreateRequest abcr = AssetBundle.LoadFromFileAsync(path);
        //return abcr;
        //callback(abcr.assetBundle);

        //Debug.Log("加载bundle名称:" + abname + " 结束:" + DateTime.Now.Ticks);



        AssetBundleRef abRef = new AssetBundleRef(path, abname, 1);
        abRef.assetBundle = bundle;
        AddAssetBundle(abname, abRef);
        return(true);
    }
Example #25
0
    // 异步GatAB,允许回调为空
    public static void GetABAsync(string assetbundleName, Action <AssetBundleRef> callback = null)
    {
        AssetBundleRef abRef;

        if (abDict.TryGetValue(assetbundleName, out abRef))
        {
            // 缓存命中
            abRef.Retain();
            if (callback != null)
            {
                callback(abRef);
            }
        }
        else
        {
            AssetLoading <AssetBundleRef> loading;
            // 检查是否已在loading
            if (loadingDict.TryGetValue(assetbundleName, out loading))
            {
                //将本次回调添加到load回调就完事了
                if (callback != null)
                {
                    loading.callback.Add(callback);
                }
                loading.refCount++;
                return;
            }

            loading = new AssetLoading <AssetBundleRef>();
            loadingDict.Add(assetbundleName, loading);
            if (callback != null)
            {
                loading.callback.Add(callback);
            }
            // 异步加载
            AssetBundleLoader.LoadAsync(assetbundleName, (assetbundle) =>
            {
                abRef = new AssetBundleRef(assetbundleName, assetbundle);
                CheckShouldCacheSpriteAsync(abRef, () =>
                {
                    CacheAB(abRef);
                    loadingDict.Remove(assetbundleName);
                    abRef.Retain(loading.refCount - 1);  // ref默认是1,所以只需retain refCount - 1
                    loading.FireCallBack(abRef);
                });
            });
        }
    }
Example #26
0
    public static IEnumerator LoadGameObject(string folder, string assetName, System.Type type)   // 錯誤 要加一個 floder
    {
        //Debug.Log("( 1 ) :" + assetName);
        AssetBundleRef abRef;
        string         assetPath = folder + assetName;

        if (!bLoadedAssetbundle(assetName))
        {
            while (_isLoadPrefab == false)
            {
                yield return(null);
            }
            _isStartLoadAsset = true;
            //Debug.Log("(2)New Path:" + Application.persistentDataPath + "/AssetBundles/" + assetName + Global.ext);
            WWW www = WWW.LoadFromCacheOrDownload("file:///" + Application.persistentDataPath + "/AssetBundles/" + assetPath + Global.ext, 1);
            _progress = (int)www.progress * 100;
            yield return(www);

            _ReturnMessage = "正再載入遊戲物件... ( " + assetPath + Global.ext + " )";
            //Debug.Log("( 2 ) :" + assetName);
            if (www.error != null)
            {
                _Ret           = "C002";
                _ReturnMessage = "載入遊戲物件失敗! :" + assetPath + "\n" + www.error;
                Debug.Log("( 3 ) :" + _ReturnMessage);
                throw new Exception(www.error);
            }
            else if (www.isDone)
            {
                _Ret              = "C001";
                _ReturnMessage    = "載入遊戲物件完成" + "( " + assetPath + " )";
                abRef             = new AssetBundleRef();
                abRef.assetBundle = www.assetBundle;
                dictAssetBundleRefs.Add(assetName, abRef);
                _request      = abRef.assetBundle.LoadAsync(assetName, type);
                _isLoadObject = true;

                www.Dispose();
                //Debug.Log("( 4 ) :" + assetName);
            }
        }
        else // 已經載入了 不須載入
        {
            _isLoadObject = true;
        }

        _loadedObjectCount++;// 這非常可能導致錯誤 應放在www.Done可是他不會計算多次的IEnumerator累計直 3+3=6 會變成只有3
    }
Example #27
0
 public static void ClearCache()
 {
     Caching.CleanCache();
     using (Dictionary <String, AssetBundleRef> .Enumerator enumerator = DictAssetBundleRefs.GetEnumerator())
     {
         while (enumerator.MoveNext())
         {
             AssetBundleRef assetBundleRef = enumerator.Current.Value;
             if (assetBundleRef.assetBundle != null)
             {
                 assetBundleRef.assetBundle.Unload(true);
                 assetBundleRef.assetBundle = null;
             }
         }
     }
 }
Example #28
0
 // Download an AssetBundle
 public static IEnumerator downloadAssetBundle(string url, int version)
 {
     string keyName = url + version.ToString ();
     if (dictAssetBundleRefs.ContainsKey (keyName))
         yield return null;
     else {
         using (WWW www = WWW.LoadFromCacheOrDownload (url, version)) {
             yield return www;
             if (www.error != null)
                 throw new Exception ("WWW download:" + www.error);
             AssetBundleRef abRef = new AssetBundleRef (url, version);
             abRef.assetBundle = www.assetBundle;
             dictAssetBundleRefs.Add (keyName, abRef);
         }
     }
 }
    // Download an AssetBundle

    /*
     * public static IEnumerator downloadAssetBundle(string url, int version)
     * {
     *  string keyName = url + version.ToString();
     *  if (dictAssetBundleRefs.ContainsKey(keyName))
     *      yield return null;
     *  else
     *  {
     *      while (!Caching.ready)
     *          yield return null;
     *
     *      using (WWW www = WWW.LoadFromCacheOrDownload(url, version))
     *      {
     *          yield return www;
     *          if (www.error != null)
     *              throw new Exception("WWW download:" + www.error);
     *          AssetBundleRef abRef = new AssetBundleRef(url, version);
     *          abRef.assetBundle = www.assetBundle;
     *          dictAssetBundleRefs.Add(keyName, abRef);
     *      }
     *  }
     * }
     */

    // Load an AssetBundle from project files
    public static void LoadAssetBundle(string url)
    {
        if (dictAssetBundleRefs.ContainsKey(url))
        {
            return;
        }
        else
        {
            AssetBundleRef abRef      = new AssetBundleRef();
            AssetBundle    sfx_bundle = AssetBundle.LoadFromFile(System.IO.Path.Combine(Application.streamingAssetsPath, url));

            abRef.assetBundle = sfx_bundle;
            dictAssetBundleRefs.Add(url, abRef);

            return;
        }
    }
Example #30
0
        public static void collectGarbage()
        {
            if (_refslist.Count == 0)
            {
                return;
            }

            for (int i = 0, n = _refslist.Count; i < n; i++)
            {
                AssetBundleRef abRef = _refslist[i];
                if ((Time.realtimeSinceStartup - abRef.lastUsedTime) >= timeout)
                {
                    _refslist.RemoveAt(i);
                    unload(abRef.url, abRef.version, false);
                    break;
                }
            }
        }
Example #31
0
    public override Atlas LoadAtlas(string spriteName)
    {
        GameBundleInfo data;

        // 检查资源清单是否有该资源
        if (CheckRes(GameResType.Sprite, spriteName, out data))
        {
            Atlas atlas;
            if (!SpriteManager.TryGetAtlas(spriteName, out atlas))
            {
                AssetBundleRef abRef = AssetBundleManager.GetAB(data.assetbundle);
                if (!abrDict.ContainsKey(data.assetbundle))
                {
                    abrDict.Add(data.assetbundle, abRef);
                }
                atlas = abRef.atlas;
            }
            return(atlas);
        }
        return(null);
    }
Example #32
0
    IEnumerator LoadAsyncCoroutineBundle(string path, string type)
    {
        if (path == null)
        {
            totalCount = 0;
            isLoad     = false;//当前阶段bundle加载完成
            yield return(null);
        }
        AssetBundleCreateRequest acr    = AssetBundle.LoadFromFileAsync(path);
        AssetBundleRef           bundle = new AssetBundleRef(path);

        bundle.Progress = acr.progress;
        bundle.abcr     = acr;
        if (!AbcacheRefDic.ContainsKey(type))
        {
            AbcacheRefDic.Add(type, bundle);
        }
        yield return(acr);

        if (acr != null)
        {
            if (Application.isEditor)
            {
                Debug.LogError("bundle读取完成:" + type);
            }
            bundle.bundle = acr.assetBundle;
            if (BundleQue.Count > 0)
            {
                string nextBundle = BundleQue.Dequeue();
                //从字典取出对应的类型
                foreach (string st in bundleDic.Keys)
                {
                    if (nextBundle.Equals(bundleDic[st]))
                    {
                        StartCoroutine(LoadAsyncCoroutineBundle(nextBundle, st));
                    }
                }
            }
        }
    }
Example #33
0
        // Download an AssetBundle
        public static IEnumerator DownloadAssetBundle(string url, uint version)
        {
            string keyName = url + version.ToString();

            if (dictAssetBundleRefs.ContainsKey(keyName))
            {
                yield return(null);
            }
            else
            {
                while (!Caching.ready)
                {
                    yield return(null);
                }

#if UNITY_2018_1_OR_NEWER
                using (UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url, version, 0))
#else
                using (UnityWebRequest uwr = UnityWebRequest.GetAssetBundle(url, version, 0))
#endif
                {
                    yield return(uwr.SendWebRequest());

                    if (uwr.isNetworkError || uwr.isHttpError)
                    {
                        Debug.Log(uwr.error);
                    }
                    else
                    {
                        // Get downloaded asset bundle
                        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);

                        AssetBundleRef abRef = new AssetBundleRef(url, version);
                        abRef.assetBundle = bundle;
                        dictAssetBundleRefs.Add(keyName, abRef);
                    }
                }
            }
        }
Example #34
0
    public AssetBundleData createAssetBundle(string path, ProgressFunc func, ClearOnSceneSwitch clearOnSceneSwitch)
    {
        LoggerHelper.Debug("createAssetBundle:" + path);
        AssetBundleRef ref2 = null;

        if (this.assetBundleMaps_.TryGetValue(path, out ref2))
        {
            ref2.AddRef();
            return(ref2.CreateData());
        }
        if (NoDoneAssetBundleDic.TryGetValue(path, out ref2))
        {
            NoDoneAssetBundleDic.Remove(path);
            ref2.AddRef();
            this.assetBundleMaps_[path] = ref2;
            return(ref2.CreateData());
        }
        ref2 = AssetBundleRef.Create(path, func, clearOnSceneSwitch);
        this.assetBundleMaps_[path] = ref2;
        ref2.AddRef();
        return(ref2.CreateData());
    }
Example #35
0
    public static IEnumerator DownloadAssetBundle(string url, int version)
    {
        string keyName = url + version.ToString();

        if (dictAssetBundleRefs.ContainsKey(keyName) || isLoading)
        {
            yield return(null);
        }
        else
        {
            isLoading = true;
            AssetBundleRef           abRef   = new AssetBundleRef(url, version);
            AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(url);
            while (!request.isDone)
            {
                yield return(null);
            }
            abRef.assetBundle = request.assetBundle;
            dictAssetBundleRefs.Add(keyName, abRef);
            isLoading = false;
        }
    }
Example #36
0
    public static IEnumerator downloadAssetBundle()
    {
        string getBundleUrl  = rootData.serverInfo ["getBundleUrl"].ToString();
        string bundleVersion = rootData.serverInfo ["bundleVersion"].ToString();
        int    bVersion      = int.Parse(bundleVersion);
        string keyName       = getBundleUrl + bundleVersion.ToString();

        if (dictAssetBundleRefs.ContainsKey(keyName))
        {
            yield return(null);
        }
        else
        {
            WWW www = WWW.LoadFromCacheOrDownload(getBundleUrl, bVersion);

            while (!www.isDone)
            {
                if (www.error != null)
                {
                    throw new Exception("WWW download :" + www.error);
                }

                rootData.fillAmount = www.progress;
                yield return(null);
            }

            AssetBundleRef abRef = new AssetBundleRef(getBundleUrl, bVersion);

            if (www.isDone && www.error == null)
            {
                rootData.fillAmount = 1;
                abRef.assetBundle   = www.assetBundle;
                dictAssetBundleRefs.Add(keyName, abRef);
            }
        }
    }
        /// <summary>
        /// Downloads the asset bundle.
        /// </summary>
        /// <returns>The asset bundle.</returns>
        /// <param name="url">URL.</param>
        /// <param name="version">Version.</param>
        /// <param name="finished">Finished.</param>
        public IEnumerator DownloadAssetBundle(string url, int version, FinishedDelegate finished)
        {
            string keyName = url + version.ToString ();
            if (dictionaryAssetBundleRef.ContainsKey (keyName)) {
                yield return null;
            } else {
                while (!Caching.ready) {
                    yield return null;
                }

                using (WWW www = WWW.LoadFromCacheOrDownload (url, version)) {
                    while (!www.isDone) {
                        progress = www.progress;
                        yield return null;
                    }
                    if (www.error != null) {
            //                        HDebug.LogError (www.error);
                    } else {
                        AssetBundleRef assetBundleRef = new AssetBundleRef (url, version);
                        assetBundleRef.assetBundle = www.assetBundle;
                        dictionaryAssetBundleRef.Add (keyName, assetBundleRef);

                        yield return null;
                    }

                    if (finished != null) {
                        finished (www);
                    }

                    progress = 1f;
                    www.Dispose ();
                }
            }
        }
		/*--------------------------------------------------*/
		/// <summary>
		/// 保存先がなければ削除を行う.
		/// </summary>
		/// <param name="bundleName"> バンドル名 </param>
		/*--------------------------------------------------*/
		static void CreateRef(string bundleName)
		{
			if (!instance.downloadList.ContainsKey(bundleName))
			{
				// データの取得.
				BundleDataList data = instance.dataList.Where((a, b) => { return a.bundleName == bundleName; }).First();
				// 保存先の作成.
				AssetBundleRef temp = new AssetBundleRef(data.URL, data.version, data.crc);
				instance.downloadList.Add(bundleName, temp);
				// ダウンロード.
				if (data.type == BundleLoadType.Local)
				{ CallNewLocalDownload(bundleName); }
				else
				{ CallNewDownload(bundleName); }
			}
		}