public void SetProjectPath(string inProjectID) { projectID = inProjectID; #if UNITY_IPHONE || UNITY_IOS projectPath = projectID + "/IOS/"; commonPath = "common2/IOS/"; serverProjectInfoFinalUrl = projectInfoServerUrl + projectInfoAddUrl + projectID; imageFinalUrl = projectInfoServerUrl + imageAddUrl; serverAssetBundlePath = assetBundleServerUrl + assetBundleAddUrl + projectPath; serverCommonAssetBundlePath = assetBundleServerUrl + assetBundleAddUrl + commonPath; #elif UNITY_ANDROID projectPath = projectID + "/Andriod/"; commonPath = "common2/Andriod/"; serverProjectInfoFinalUrl = projectInfoServerUrl + projectInfoAddUrl + projectID; imageFinalUrl = projectInfoServerUrl + imageAddUrl; serverAssetBundlePath = assetBundleServerUrl + assetBundleAddUrl + projectPath; serverCommonAssetBundlePath = assetBundleServerUrl + assetBundleAddUrl + commonPath; #endif if (!Directory.Exists(Application.persistentDataPath + "/" + projectID.ToString())) { Directory.CreateDirectory(Application.persistentDataPath + "/" + projectID.ToString()); } if (!Directory.Exists(Application.persistentDataPath + "/" + projectID.ToString() + "/imageCache")) { Directory.CreateDirectory(Application.persistentDataPath + "/" + projectID.ToString() + "/imageCache"); } localProjectInfoPath = Application.persistentDataPath + "/" + projectID.ToString() + "/ProjectInfo.txt"; localProjectAssetBundlesInfoPath = Application.persistentDataPath + "/" + projectID.ToString() + "/ProjectAssetBundlesInfo.txt"; localImageCachePath = Application.persistentDataPath + "/" + projectID.ToString() + "/imageCache"; localHXinfoCachePath = Application.persistentDataPath + "/HXinfoCache"; serverProjectAssetBundlesInfoPath = serverAssetBundlePath + "ProjectAssetBundlesInfo.txt"; GlobalDebug.ReplaceLine("Loacl PersistentDataPath: " + Application.persistentDataPath, 8); GlobalDebug.ReplaceLine("CacheCount:" + Caching.cacheCount.ToString(), 9); GlobalDebug.ReplaceLine(Caching.GetCacheAt(0).path, 10); GlobalDebug.ReplaceLine("Loacl " + localProjectAssetBundlesInfoPath, 11); GlobalDebug.ReplaceLine("Servrl " + serverProjectAssetBundlesInfoPath, 12); }
public static int GetCacheAt_s(IntPtr l) { int result; try { int cacheIndex; LuaObject.checkType(l, 1, out cacheIndex); Cache cacheAt = Caching.GetCacheAt(cacheIndex); LuaObject.pushValue(l, true); LuaObject.pushValue(l, cacheAt); result = 2; } catch (Exception e) { result = LuaObject.error(l, e); } return(result); }
private void SetupCache() { string folderName = "SnycedAssetBundle"; var path = System.IO.Path.Combine(Application.persistentDataPath, folderName); if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } Cache newCache = Caching.AddCache(path); if (newCache.valid) { Caching.currentCacheForWriting = newCache; } //delete oldest caching while (Caching.cacheCount > 5) { Caching.RemoveCache(Caching.GetCacheAt(1)); } }
IEnumerator DownloadAndCacheAssetBundle(string uri, string manifestBundlePath) { //Load the manifest AssetBundle manifestBundle = AssetBundle.LoadFromFile(manifestBundlePath); AssetBundleManifest manifest = manifestBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); //Create new cache string today = DateTime.Today.ToLongDateString(); Directory.CreateDirectory(today); Cache newCache = Caching.AddCache(today); //Set current cache for writing to the new cache if the cache is valid if (newCache.valid) { Caching.currentCacheForWriting = newCache; } //Download the bundle Hash128 hash = manifest.GetAssetBundleHash("bundleName"); UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri, hash, 0); yield return(request.SendWebRequest()); AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request); //Get all the cached versions List <Hash128> listOfCachedVersions = new List <Hash128>(); Caching.GetCachedVersions(bundle.name, listOfCachedVersions); if (!AssetBundleContainsAssetIWantToLoad(bundle)) //Or any conditions you want to check on your new asset bundle { //If our criteria wasn't met, we can remove the new cache and revert back to the most recent one Caching.currentCacheForWriting = Caching.GetCacheAt(Caching.cacheCount); Caching.RemoveCache(newCache); for (int i = listOfCachedVersions.Count - 1; i > 0; i--) { //Load a different bundle from a different cache request = UnityWebRequestAssetBundle.GetAssetBundle(uri, listOfCachedVersions[i], 0); yield return(request.SendWebRequest()); bundle = DownloadHandlerAssetBundle.GetContent(request); //Check and see if the newly loaded bundle from the cache meets your criteria if (AssetBundleContainsAssetIWantToLoad(bundle)) { break; } } } else { //This is if we only want to keep 5 local caches at any time if (Caching.cacheCount > 5) { Caching.RemoveCache(Caching.GetCacheAt(1)); //Removes the oldest user created cache } } }
//public void LoadFromLocal() //{ // string path = ""; // path = "file://" + Application.persistentDataPath + "/ar-exp-saltbrochure-android"; // //path = "file://" + "C:/Users/sricciardi/Desktop/Projects/AR Projects/Salt-Generic-AR-App/Assets/AssetBundles/Android" + "/ar-exp-saltbrochure-android"; // Debug.Log(path); // StartCoroutine(LoadFromMemoryAsync(path)); //} IEnumerator DownloadAndCacheAB(string bundleURLPath, string manifestURLBundlePath) { //AssetBundle manifestBundle = AssetBundle.LoadFromFile(manifestBundlePath); //AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest"); string editorManifestFilePath = Application.dataPath + "/AssetBundles/Android/" + ABFileName + ".manifest"; string manifestFilePath = Path.Combine(Application.persistentDataPath, ABmanifestFileName); string bundleFilePath = Path.Combine(Application.persistentDataPath, ABFileName); Debug.Log("Unity Editor manifest path " + editorManifestFilePath); if (Application.isEditor) { DHmanifest = new DownloadHandlerFile(editorManifestFilePath); Debug.Log("I am running in the Unity Editor!"); } else { DHmanifest = new DownloadHandlerFile(manifestFilePath); } //DownloadHandlerFile dh = new DownloadHandlerFile(filePath); //DownloadHandlerAssetBundle DHAB = new DownloadHandlerAssetBundle(bundleFilePath); //DownloadHandler DHmanifest = new DownloadHandlerFile(manifestFilePath); // Correct! UWRmanifest = UnityWebRequest.Get(manifestURLBundlePath); //UWRbundle = UnityWebRequestAssetBundle.GetAssetBundle(bundleURLPath); //uwr.downloadHandler = dhAB; UWRmanifest.downloadHandler = DHmanifest; yield return(UWRmanifest.SendWebRequest()); //UWRbundle.downloadHandler = DHAB; //yield return UWRbundle.SendWebRequest(); //uwr.downloadHandler = dh; //yield return uwr.SendWebRequest(); if (UWRmanifest.isNetworkError || UWRmanifest.isHttpError) { Debug.Log("Network error trying to retrieve the manifest file!"); } else { Debug.Log("There was no Network error, so try to load the Asset Bundle from the local file"); AssetBundle manifestBundle = AssetBundle.LoadFromFile(manifestFilePath); AssetBundleManifest manifest = manifestBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); //Create new cache string today = DateTime.Today.ToLongDateString(); Directory.CreateDirectory(today); Cache newCache = Caching.AddCache(today); //Set current cache for writing to the new cache if the cache is valid if (newCache.valid) { Caching.currentCacheForWriting = newCache; } //Download the bundle Hash128 hash = manifest.GetAssetBundleHash(ABFileName); UnityWebRequest UWRbundleRequest = UnityWebRequestAssetBundle.GetAssetBundle(bundleURLPath, hash, 0); yield return(UWRbundleRequest.SendWebRequest()); AssetBundle UWRbundle = DownloadHandlerAssetBundle.GetContent(UWRbundleRequest); //Get all the cached versions List <Hash128> listOfCachedVersions = new List <Hash128>(); Caching.GetCachedVersions(UWRbundle.name, listOfCachedVersions); if (!AssetBundleContainsAssetIWantToLoad(UWRbundle)) { Caching.currentCacheForWriting = Caching.GetCacheAt(Caching.cacheCount); Caching.RemoveCache(newCache); Debug.Log("There is no Scene in the downloaded Asset Bundle."); for (int i = listOfCachedVersions.Count - 1; i > 0; i--) { //Load a different bundle from a different cache. UWRbundleRequest = UnityWebRequestAssetBundle.GetAssetBundle(bundleURLPath, listOfCachedVersions[i], 0); yield return(UWRbundleRequest.SendWebRequest()); UWRbundle = DownloadHandlerAssetBundle.GetContent(UWRbundleRequest); //Check and see if the newly loaded bundle from the cache meets your criteria. if (AssetBundleContainsAssetIWantToLoad(UWRbundle)) { //This is where I need to load the scene the correct way. break; } } } else { Debug.Log("I found the scene I am looking for in the downloaded version."); } } /* * if (uwr.isNetworkError || uwr.isHttpError) * { * Debug.Log(uwr.error); * } else * { * //Get Asset Bundle * AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr); * * isLoading = true; * * if (uwr.isDone) * { * * //AssetBundleManifest manifest = bundle.LoadAsset<AssetBundleManifest>(bundle.name); * Debug.Log("bundle name " + bundle.name); * Debug.Log("is Streamed Asset Bundle " + bundle.isStreamedSceneAssetBundle); * } * * * //AssetBundle manifestBundle = AssetBundle.LoadFromFile(manifestBundlePath); * //AssetBundleManifest manifest = bundle.LoadAssetAsync<AssetBundleManifest>(ABFileName); * //Debug.Log("manifest " + manifest); * * string[] assetBundlePaths = bundle.GetAllScenePaths(); * * if (assetBundlePaths != null) { * * //AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(sceneAssetBundle, levelName, isAdditive); * //if (request == null) * // yield break; * //yield return StartCoroutine(request); * * for (int i = 0; i < assetBundlePaths.Length; i++) * { * Debug.Log("string # " + i + " is " + assetBundlePaths[i]); * } * } */ //string filePath = Path.Combine(Application.persistentDataPath, ABFileName); //DownloadHandlerFile dh = new DownloadHandlerFile(filePath); //UnityWebRequest requestFile = UnityWebRequest.Get(url); //requestFile.downloadHandler = dh; //yield return requestFile.SendWebRequest(); //if (!requestFile.isHttpError && !requestFile.isNetworkError) //{ // Debug.Log("Download complete, attempting to load bundle."); // AssetBundle bundleFile = AssetBundle.LoadFromFile(filePath); // SceneManager.LoadScene(bundleFile.GetAllScenePaths()[0]); //} //else //{ // Debug.LogError(requestFile.responseCode + ": " + requestFile.error); //} // Load level from assetBundle. ////AssetBundleRequest asset = bundle.LoadAssetAsync<AssetBundleManifest>(ABFileName); //AssetBundleRequest asset = bundle.LoadAsset<AssetBundleManifest>(ABFileName); //yield return asset; ////Get the Asset Bundle Manifest //AssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest; ////Get Hash128 from the AssetBundleManifest //Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash(ABFileName); ////Pass to the isVersionCached function //if (Caching.IsVersionCached(bundlePath, tempHash128)) //{ // isCached = true; // Debug.Log("Bundle is Cached"); //} else //{ // isLoading = true; // Debug.Log("Bundle is Downloading...."); //} ////Caching.IsVersionCached(bundlePath, tempHash128); }