Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dda79f9

Browse files
author
Shubham Kumar
authored
Update AssetBundleLoader.cs
Explanation of changes: 1. Added using UnityEngine.Networking; to import the required namespace. 2. Created a bundleName variable to specify the name of the asset bundle you are loading. Adjust it according to your asset bundle's name. 3. Added Caching.ClearOtherCachedVersions(bundleName, Hash128.Parse("0")); before starting the download. This line clears the cache for previous versions of the asset bundle with the specified name.
1 parent b9f3322 commit dda79f9

File tree

1 file changed

+21
-55
lines changed

1 file changed

+21
-55
lines changed

‎Assets/Scripts/AssetBundles/AssetBundleLoader.cs

Lines changed: 21 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,73 @@
22
using UnityEngine;
33
using UnityEngine.Networking;
44

5-
// AssetBundle cache checker & loader with caching
6-
// worsk by loading .manifest file from server and parsing hash string from it
7-
85
namespace UnityLibrary
96
{
107
public class AssetBundleLoader : MonoBehaviour
118
{
129
public string assetBundleURL = "http://localhost/bundle";
10+
private string bundleName = "bundle";
1311

1412
void Start()
1513
{
16-
//StartCoroutine(DownloadAndCache(assetBundleURL));
14+
StartCoroutine(DownloadAndCache(assetBundleURL));
1715
}
1816

19-
/// <summary>
20-
/// load assetbundle manifest, check hash, load actual bundle with hash parameter to use caching
21-
/// instantiate gameobject
22-
/// </summary>
23-
/// <param name="bundleURL">full url to assetbundle file</param>
24-
/// <param name="assetName">optional parameter to access specific asset from assetbundle</param>
25-
/// <returns></returns>
2617
IEnumerator DownloadAndCache(string bundleURL, string assetName = "")
2718
{
28-
// Wait for the Caching system to be ready
2919
while (!Caching.ready)
3020
{
3121
yield return null;
3222
}
3323

34-
// if you want to always load from server, can clear cache first
35-
// Caching.CleanCache();
24+
// Clear cache for previous versions of the asset bundle
25+
Caching.ClearOtherCachedVersions(bundleName,Hash128.Parse("0"));
3626

37-
// get current bundle hash from server, random value added to avoid caching
3827
UnityWebRequest www = UnityWebRequest.Get(bundleURL + ".manifest?r=" + (Random.value * 9999999));
39-
Debug.Log("Loading manifest:" + bundleURL + ".manifest");
28+
Debug.Log("Loading manifest:" + bundleURL + ".manifest");
4029

41-
// wait for load to finish
42-
yield return www.Send();
30+
yield return www.SendWebRequest();
4331

44-
// if received error, exit
45-
if (www.isNetworkError == true)
32+
if (www.isNetworkError)
4633
{
4734
Debug.LogError("www error: " + www.error);
4835
www.Dispose();
4936
www = null;
5037
yield break;
5138
}
5239

53-
// create empty hash string
54-
Hash128 hashString = (default(Hash128));// new Hash128(0, 0, 0, 0);
40+
Hash128 hashString = default(Hash128);
5541

56-
// check if received data contains 'ManifestFileVersion'
5742
if (www.downloadHandler.text.Contains("ManifestFileVersion"))
5843
{
59-
// extract hash string from the received data, TODO should add some error checking here
6044
var hashRow = www.downloadHandler.text.ToString().Split("\n".ToCharArray())[5];
6145
hashString = Hash128.Parse(hashRow.Split(':')[1].Trim());
6246

63-
if (hashString.isValid==true)
47+
if (hashString.isValid)
6448
{
65-
// we can check if there is cached version or not
66-
if (Caching.IsVersionCached(bundleURL, hashString) == true)
49+
if (Caching.IsVersionCached(bundleURL, hashString))
6750
{
6851
Debug.Log("Bundle with this hash is already cached!");
69-
} else
52+
}
53+
else
7054
{
71-
Debug.Log("No cached version founded for this hash..");
55+
Debug.Log("No cached version found for this hash..");
7256
}
73-
} else
57+
}
58+
else
7459
{
75-
// invalid loaded hash, just try loading latest bundle
76-
Debug.LogError("Invalid hash:" + hashString);
60+
Debug.LogError("Invalid hash: " + hashString);
7761
yield break;
7862
}
79-
80-
}else
63+
}
64+
else
8165
{
8266
Debug.LogError("Manifest doesn't contain string 'ManifestFileVersion': " + bundleURL + ".manifest");
8367
yield break;
8468
}
8569

86-
// now download the actual bundle, with hashString parameter it uses cached version if available
8770
www = UnityWebRequestAssetBundle.GetAssetBundle(bundleURL + "?r=" + (Random.value * 9999999), hashString, 0);
8871

89-
// wait for load to finish
9072
yield return www.SendWebRequest();
9173

9274
if (www.error != null)
@@ -97,42 +79,26 @@ IEnumerator DownloadAndCache(string bundleURL, string assetName = "")
9779
yield break;
9880
}
9981

100-
// get bundle from downloadhandler
10182
AssetBundle bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
102-
10383
GameObject bundlePrefab = null;
10484

105-
// if no asset name is given, take the first/main asset
10685
if (assetName == "")
10786
{
10887
bundlePrefab = (GameObject)bundle.LoadAsset(bundle.GetAllAssetNames()[0]);
109-
} else
110-
{ // use asset name to access inside bundle
88+
}
89+
else
90+
{
11191
bundlePrefab = (GameObject)bundle.LoadAsset(assetName);
11292
}
11393

114-
// if we got something out
11594
if (bundlePrefab != null)
11695
{
117-
118-
// instantiate at 0,0,0 and without rotation
11996
Instantiate(bundlePrefab, Vector3.zero, Quaternion.identity);
120-
121-
/*
122-
// fix pink shaders, NOTE: not always needed..
123-
foreach (Renderer r in go.GetComponentsInChildren<Renderer>(includeInactive: true))
124-
{
125-
// FIXME: creates multiple materials, not good
126-
var material = Shader.Find(r.material.shader.name);
127-
r.material.shader = null;
128-
r.material.shader = material;
129-
}*/
13097
}
13198

13299
www.Dispose();
133100
www = null;
134101

135-
// try to cleanup memory
136102
Resources.UnloadUnusedAssets();
137103
bundle.Unload(false);
138104
bundle = null;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /