Skip to content

Commit 86d1991

Browse files
authored
Create AssetBundleLoader.cs
1 parent a3b1875 commit 86d1991

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
using UnityEngine.Networking;
4+
5+
// example script to load hash from manifest file
6+
// then use that hash for asset bundle loading (to use cached version when available)
7+
8+
namespace UnityLibrary
9+
{
10+
public class AssetBundleLoader : MonoBehaviour
11+
{
12+
public string assetBundleURL = "http://localhost/bundle";
13+
14+
void Start()
15+
{
16+
StartCoroutine(DownloadAndCache(assetBundleURL, ""));
17+
}
18+
19+
20+
/// <summary>
21+
/// asset bundle load and instantiate
22+
/// </summary>
23+
/// <param name="bundleURL">full url to the bundle file</param>
24+
/// <param name="assetName">optional asset name to instantiate from the bundle</param>
25+
IEnumerator DownloadAndCache(string bundleURL, string assetName)
26+
{
27+
// Wait for the Caching system to be ready
28+
while (!Caching.ready)
29+
{
30+
yield return null;
31+
}
32+
33+
// if you want to always load from server, can clear cache first
34+
// Caching.CleanCache();
35+
36+
// get current bundle hash from server, random value added to avoid caching
37+
UnityWebRequest www = UnityWebRequest.Get(bundleURL + ".manifest?r=" + (Random.value * 9999999));
38+
Debug.Log("Loading manifest:" + bundleURL + ".manifest");
39+
40+
// wait for load to finish
41+
yield return www.Send();
42+
43+
// if received error, exit
44+
if (www.isError == true)
45+
{
46+
Debug.LogError("www error:" + www.error);
47+
yield break;
48+
}
49+
50+
// create empty hash string
51+
Hash128 hashString = (default(Hash128));// new Hash128(0, 0, 0, 0);
52+
53+
// check if received data contains 'ManifestFileVersion'
54+
if (www.downloadHandler.text.Contains("ManifestFileVersion"))
55+
{
56+
// extract hash string from the received data, should add some error checking here
57+
var hashRow = www.downloadHandler.text.ToString().Split("\n".ToCharArray())[5];
58+
hashString = Hash128.Parse(hashRow.Split(':')[1].Trim());
59+
60+
if (hashString.isValid == true)
61+
{
62+
if (Caching.IsVersionCached(bundleURL, hashString) == true)
63+
{
64+
Debug.Log("Bundle with this hash is already cached!");
65+
} else
66+
{
67+
Debug.Log("No cached version founded for this hash..");
68+
}
69+
} else
70+
{
71+
// invalid loaded hash, just try loading latest bundle
72+
Debug.LogError("Invalid hash:" + hashString);
73+
yield break;
74+
}
75+
76+
} else
77+
{
78+
Debug.LogError("Manifest doesn't contain string 'ManifestFileVersion': " + bundleURL + ".manifest");
79+
yield break;
80+
}
81+
82+
83+
84+
// now download the actual bundle, with hashString parameter it uses cached version if available
85+
www = UnityWebRequest.GetAssetBundle(bundleURL + "?r=" + (Random.value * 9999999), hashString, 0);
86+
87+
// wait for load to finish
88+
yield return www.Send();
89+
90+
if (www.error != null)
91+
{
92+
Debug.LogError("www error:" + www.error);
93+
yield break;
94+
}
95+
96+
// get bundle from downloadhandler
97+
AssetBundle bundle = ((DownloadHandlerAssetBundle)www.downloadHandler).assetBundle;
98+
99+
GameObject bundlePrefab = null;
100+
101+
// if no asset name is given, take the first/main asset
102+
if (assetName == "")
103+
{
104+
bundlePrefab = (GameObject)bundle.LoadAsset(bundle.GetAllAssetNames()[0]);
105+
} else // use asset name
106+
{
107+
bundlePrefab = (GameObject)bundle.LoadAsset(assetName);
108+
}
109+
110+
// if we got something out
111+
if (bundlePrefab != null)
112+
{
113+
// instantiate at 0,0,0 and without rotation
114+
var go = Instantiate(bundlePrefab, Vector3.zero, Quaternion.identity) as GameObject;
115+
116+
/*
117+
// fix pink shaders, NOTE: not always needed..
118+
foreach (Renderer r in go.GetComponentsInChildren<Renderer>(includeInactive: true))
119+
{
120+
// FIXME: creates multiple materials, not good
121+
var material = Shader.Find(r.material.shader.name);
122+
r.material.shader = null;
123+
r.material.shader = material;
124+
}*/
125+
}
126+
127+
bundle.Unload(false);
128+
}
129+
}
130+
131+
}

0 commit comments

Comments
 (0)