Blu*_*tar 3 c# unity-game-engine assetbundle
我想知道 AssetBundle 已经在缓存中。这通常是通过Caching.IsVersionCached函数完成的。
该Caching.IsVersionCached(string url, int version)函数重载是不再支持统一2017.1。
Unity 2017.3建议我使用Caching.IsVersionCached(string url, Hash128 hash)重载。
我不知道它Hash128是什么以及如何获取和使用它。什么是Hash128用于如何从 AssetBundle 中获取它的?
谢谢您的回答。
但我无法解决我的问题。
这是我的代码
for (int i = 0; i < assetInfoList.Count; i++) {
while (!Caching.ready)
yield return null;
string url = GetUrl(assetInfoList[i].assetbundle_name);
string keyName = url + assetInfoList[i].version_no.ToString();
using (UnityWebRequest request = UnityWebRequest.GetAssetBundle(url,(uint)assetInfoList[i].version_no, 0))
{
if (Caching.IsVersionCached(url, assetInfoList[i].version_no) == true)
continue;
if (i == 0)
{
string wifiConnectRecommend = Message.Ins.WIFI_ONLY;
PopUpManager.Instance.PopUp_YesORNoForAssetBundle(wifiConnectRecommend.ToString(), ClickWifi, availableBytes, totalBytes);
while (!SceneManager.Ins.isWifiUseConfirm)
yield return null;
}
request.SendWebRequest();
while (request.isDone == false)
{
progressbar.value = request.downloadProgress;
currentCount.text = countString + " (" + (request.downloadProgress * 100).ToString("N2") + "%)";
yield return null;
}
if (request.error != null)
{
Debug.Log("www.error");
}
else
{
AssetBundleRef abRef = new AssetBundleRef(url, assetInfoList[i].version_no);
abRef.assetBundle = DownloadHandlerAssetBundle.GetContent(request);
dictAssetBundleRefs.Add(keyName, abRef);
}
}
Run Code Online (Sandbox Code Playgroud)
我的目的是当 assetbundle 已经在缓存中时,继续需要下载,在 PopUp_YesORNoForAssetBundle 上设置。
检查已下载的资产包或版本检查时需要 hash128。
但在您的解释中,hash128 仅在上一次资源文件夹中加载或保存清单文件后才能获得。
我想知道如何检查 assetbunle 是否在缓存中。
如果你提出建议,我真的很感谢你。
但我不知道什么是 hash128
Hash128 表示 AssetBundle 文件的哈希值,可以在下载时更轻松地比较 AssetBundle 文件版本。
我找不到如何使用 hash128
文档中没有关于如何执行此操作的示例。以下是三种获取方式Hash128。使用哪一个取决于位置的条件AssetBundle以及是否要下载以检查Hash128. 在您的情况下,#1可能是您正在寻找的。
1 . 在 AssetBundle 构建期间从编辑器获取Hash128,然后保存到资源文件夹:
使用该函数构建AssetBundle时BuildPipeline.BuildAssetBundles,该函数返回AssetBundleManifest. 您可以Hash128使用该AssetBundleManifest.GetAssetBundleHash功能获取。将 转换Hash128为字符串,Hash128.ToString()然后将其保存到 Resources 文件夹,以便您可以在运行时访问它。
将构建 AssetBundle 并将其保存到 Resources 文件夹的编辑器构建脚本示例Hash128:
[MenuItem("Assets/Build AssetBundle")]
static void ExportResource()
{
string folderName = "AssetBundles";
string filePath = Path.Combine(Application.streamingAssetsPath, folderName);
AssetBundleManifest assetMf = BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
//Get Hash128 from the AssetBundleManifest
Hash128 hash128 = assetMf.GetAssetBundleHash("AssetBundles");
//Get the Hash128 as string
string data = hash128.ToString();
string path = "Assets/Resources/AssetInfo/AssetBundleInfo.txt";
//Save the Hash128 to the Resources folder
using (FileStream fileStream = new FileStream(path, FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.Write(data);
}
}
UnityEditor.AssetDatabase.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
Hash128在运行时加载它的简单函数:
Hash128 getHash128(string path)
{
//Load from the Resources folder
TextAsset txtAsset = (TextAsset)Resources.Load(path, typeof(TextAsset));
string hash128 = txtAsset.text;
return Hash128.Parse(hash128);
}
Run Code Online (Sandbox Code Playgroud)
如何使用:
//Load Hash128 from the Resources folder
Hash128 tempHash128 = getHash128("AssetInfo/AssetBundleInfo");
//Pass to the IsVersionCached function
Caching.IsVersionCached("yourUrl", tempHash128);
Run Code Online (Sandbox Code Playgroud)
您还可以Hash128根据需要使用 json 来表示和保存许多。
2 . 从服务器下载 AssetBundle,然后Hash128在运行时获取,没有 Resources 文件夹。不幸的是,您必须先使用此方法下载 AssetBundle,然后才能获得它的Hash128. 下载后,加载数据,AssetBundleManifest然后Hash128使用该AssetBundleManifest.GetAssetBundleHash函数从中获取 。然后您可以保存它Hash128以备后用。
下面的示例应该Hash128从 AssetBundle url下载和提取。不涉及编辑器代码。
IEnumerator downloadAssetBundle(string url)
{
UnityWebRequest www = UnityWebRequest.GetAssetBundle(url);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
//Get the AssetBundle
AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);
AssetBundleRequest asset = bundle.LoadAssetAsync<AssetBundleManifest>("assetManifestName");
yield return asset;
//Get the AssetBundleManifest
AssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest;
//Get Hash128 from the AssetBundleManifest
Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash("");
//Pass to the IsVersionCached function
Caching.IsVersionCached("yourUrl", tempHash128);
}
}
Run Code Online (Sandbox Code Playgroud)
3 . 从文件系统中的 AssetBundle获取Hash128。如果您已经知道AssetBundle 的路径,请从该路径加载 AssetBundle,然后加载数据AssetBundleManifest,最后Hash128使用AssetBundleManifest.GetAssetBundleHash函数从中获取 。然后您可以保存它Hash128以备后用。
这显示了如何从 StreamingAsset 路径加载 AssetBundle 并获取其Hash128:
IEnumerator loadAssetManifest(string assetBundleName, string assetManifestName)
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles");
filePath = System.IO.Path.Combine(filePath, assetBundleName);
var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
yield return assetBundleCreateRequest;
AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;
AssetBundleRequest asset = asseBundle.LoadAssetAsync<AssetBundleManifest>(assetManifestName);
yield return asset;
//Get the AssetBundleManifest
AssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest;
//Get Hash128 from the AssetBundleManifest
Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash("");
//Pass to the IsVersionCached function
Caching.IsVersionCached("yourUrl", tempHash128);
}
Run Code Online (Sandbox Code Playgroud)