Search Unity

AssetBundle not completely cleared in iOS

Discussion in 'Scripting' started by technobayo, Jun 15, 2018.

  1. technobayo

    technobayo

    Joined:
    Apr 8, 2017
    Posts:
    13
    Hi all,

    I'm working on an app that lets the user manage levels by downloading and deleting them as they wish. I use AssetBundles for each of the levels and they are stored on my server as level1, level2, level3. I use Caching.ClearAllCachedVersions("levelname") to delete each level from the cache when a user presses delete. It worked for all previously until I added 2 new assetbundles to the server. Now, it deletes some while some remain in the cache. I know because when I turn off the internet, it still loads it. The funny thing is I'm only having this problem on iOS. Here is my code.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    using UnityEngine.Networking;
    using UnityEngine.Video;
    using UnityEngine.UI;
    using UnityEngine.SceneManagement;
    using UnityEngine.EventSystems;

    public class AssetDownload : MonoBehaviour {

    public static AssetBundle bundle;
    public static VideoClip clip1;
    public static string scene1, scene2, scene3;
    public Text textxx,infoPanelText,sliderText;
    public Button downldBtn, openBtn, deleteBtn;
    public Slider slider;

    public static AssetDownload instance;

    void Start(){
    instance = this;
    }

    public void DownloadVideo(){

    AssetBundle[] bundles = Resources.FindObjectsOfTypeAll<AssetBundle>();
    if(bundles.Length > 0)
    {
    bundles[0].Unload(true);
    }

    string strText = infoPanelText.text;
    if(strText == "SCENE 1")
    {
    StartCoroutine(InstantiateObject("level1","scene1"));
    }
    else if (strText == "SCENE 2")
    {
    StartCoroutine(InstantiateObject("level2","scene2"));
    }
    else if (strText == "SCENE 3")
    {
    StartCoroutine(InstantiateObject("level3", "scene3"));
    }
    }

    public IEnumerator InstantiateObject(string str1, string scenename)

    {
    using (UnityWebRequest uwr = UnityWebRequest.GetAssetBundle("http://www.xxxabc.com/defghi/"+str1,0,0))
    {

    UnityWebRequestAsyncOperation request = uwr.SendWebRequest();

    while (!request.isDone)
    {
    float progress = Mathf.Clamp01(request.progress / .8f);
    Debug.Log(progress);
    slider.value = progress;
    if(progress > 0)
    {
    slider.gameObject.SetActive(true);
    textxx.text = "Downloading...";
    }
    yield return null;
    }

    if (uwr.isNetworkError || uwr.isHttpError)
    {
    textxx2.text = "Error: " + uwr.error + " . Check your internet.";
    }
    else
    {
    // Get downloaded asset bundle
    bundle = DownloadHandlerAssetBundle.GetContent(uwr);

    string[] scenePaths = bundle.GetAllScenePaths();
    scenename = System.IO.Path.GetFileNameWithoutExtension(scenePaths[0]);
    textxx.text = "DOWNLOADED. Response Code Lev 1: " + uwr.responseCode;

    }
    print("Response Code Lev 1: " + uwr.responseCode);

    string url = "http://www.xxxabc.com/defghi/";
    if(uwr.responseCode == 0 && Caching.IsVersionCached(url + str1, 0))
    {
    SceneManager.LoadScene(scenename);
    }
    }
    }

    //This is called when I press delete for the level
    public void ClearCache(){

    /*
    * First we check if there is a loaded bundle before deleting
    * There can only be 1 bundle of same name cos we always unload in download
    */
    AssetBundle[] bundles = Resources.FindObjectsOfTypeAll<AssetBundle>();

    string strText = infoPanelText.text;
    if (strText == "SCENE 1")
    {
    if (bundles.Length > 0 && bundles[0].name == "level1")
    {
    bundle.Unload(true);
    }
    Caching.ClearAllCachedVersions("level1");
    }
    else if (strText == "SCENE 2")
    {
    if (bundles.Length > 0 && bundles[0].name == "level2")
    {
    bundle.Unload(true);
    }
    Caching.ClearAllCachedVersions("level2");
    }
    else if (strText == "SCENE 3")
    {
    if (bundles.Length > 0 && bundles[0].name == "level3")
    {
    bundle.Unload(true);
    }
    Caching.ClearAllCachedVersions("level3");
    }
    }
    }
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,619