本文介绍了从 AssetBundle 为 Caching.IsVersionCached 函数获取 Hash128的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 AssetBundle 已经在缓存中.这通常通过 Caching.IsVersionCached 函数完成.

Unity 2017.1 不再支持 Caching.IsVersionCached(string url, int version) 函数重载.

Unity 2017.3 建议我使用 Caching.IsVersionCached(string url, Hash128 hash) 重载.

我不知道Hash128 是什么以及如何获取和使用它.什么是 Hash128 用于如何从 AssetBundle 中获取它?

感谢您的回答.

但我无法解决我的问题.

这是我的代码

for (int i = 0; i

while (!Caching.ready)收益率返回空;string url = GetUrl(assetInfoList[i].assetbundle_name);string keyName = url + assetInfoList[i].version_no.ToString();使用 (UnityWebRequest request = UnityWebRequest.GetAssetBundle(url,(uint)assetInfoList[i].version_no, 0)){if (Caching.IsVersionCached(url, assetInfoList[i].version_no) == true)继续;如果(我== 0){字符串 wifiConnectRecommend = Message.Ins.WIFI_ONLY;PopUpManager.Instance.PopUp_YesORNoForAssetBundle(wifiConnectRecommend.ToString(), ClickWifi, availableBytes, totalBytes);while (!SceneManager.Ins.isWifiUseConfirm)收益率返回空;}request.SendWebRequest();while (request.isDone == false){progressbar.value = request.downloadProgress;currentCount.text = countString + " (" + (request.downloadProgress * 100).ToString("N2") + "%)";收益率返回空;}if (request.error != null){Debug.Log("www.error");}别的{AssetBundleRef abRef = new AssetBundleRef(url, assetInfoList[i].version_no);abRef.assetBundle = DownloadHandlerAssetBundle.GetContent(request);dictAssetBundleRefs.Add(keyName, abRef);}}

}我的目的是当 assetbundle 已经在缓存中时,继续需要下载,在 PopUp_YesORNoForAssetBundle 上设置.

检查下载的资源包或版本检查时需要hash128.

但在您的解释中,hash128 仅在上次加载资源文件夹或在资源文件夹中保存清单文件后才能获得.

我想知道如何检查assetbunle是否在缓存中.

如果你给我指路,我真的很感谢你.

解决方案

Hash128 表示 AssetBundle 文件的哈希值这使得在下载时更容易比较 AssetBundle 文件版本.

我找不到如何使用 hash128

文档中没有关于如何执行此操作的示例.以下是三种获取Hash128.使用哪一个取决于 AssetBundle 所在的位置,以及是否要下载它以查看 Hash128 与否.在您的情况下,#1 可能是您正在寻找的.

1.在 AssetBundle 构建期间从编辑器获取 Hash128,然后保存到 Resources 文件夹:

在使用 AssetBundle 时.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html" rel="noreferrer">BuildPipeline.BuildAssetBundles 函数,该函数返回AssetBundleManifest.您可以使用 Hash128 获取AssetBundleManifest.GetAssetBundleHash 函数.将 Hash128 转换为带有 Hash128.ToString() 然后将其保存到 Resources 文件夹,以便 您可以在运行时访问它.

Editor 构建脚本示例,它将构建 AssetBundle 并将 Hash128 保存到 Resources 文件夹:

[MenuItem("Assets/Build AssetBundle")]静态无效导出资源(){string folderName = "AssetBundles";string filePath = Path.Combine(Application.streamingAssetsPath, folderName);AssetBundleManifest assetMf = BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);//从AssetBundleManifest中获取Hash128Hash128 hash128 = assetMf.GetAssetBundleHash("AssetBundles");//获取Hash128作为字符串字符串数据 = hash128.ToString();string path = "Assets/Resources/AssetInfo/AssetBundleInfo.txt";//将Hash128保存到Resources文件夹使用 (FileStream fileStream = new FileStream(path, FileMode.Create)){使用 (StreamWriter writer = new StreamWriter(fileStream)){writer.Write(数据);}}UnityEditor.AssetDatabase.Refresh();}

一个在运行时加载 Hash128 的简单函数:

Hash128 getHash128(字符串路径){//从资源文件夹加载TextAsset txtAsset = (TextAsset)Resources.Load(path, typeof(TextAsset));字符串 hash128 = txtAsset.text;返回 Hash128.Parse(hash128);}

使用方法:

//从Resources文件夹加载Hash128Hash128 tempHash128 = getHash128("AssetInfo/AssetBundleInfo");//传递给IsVersionCached函数Caching.IsVersionCached("yourUrl", tempHash128);

您也可以根据需要使用 json 来表示和保存许多为 Hash128.

2.从服务器下载AssetBundle,然后在没有Resources文件夹的运行时获取Hash128.不幸的是,您必须先使用此方法下载 AssetBundle,然后才能获取其 Hash128.下载后,将数据加载为AssetBundleManifest,然后使用AssetBundleManifest.GetAssetBundleHash 函数从中获取Hash128.然后您可以保存此 Hash128 以备后用.

下面的示例应该从 AssetBundle url 下载并提取 Hash128.不涉及编辑器代码.

IEnumerator downloadAssetBundle(string url){UnityWebRequest www = UnityWebRequest.GetAssetBundle(url);收益率返回 www.SendWebRequest();if (www.isNetworkError || www.isHttpError){Debug.Log(www.error);}别的{//获取AssetBundleAssetBundle bundle = DownloadHandlerAssetBundle.GetContent(www);AssetBundleRequest asset = bundle.LoadAssetAsync("assetManifestName");收益回报资产;//获取AssetBundleManifestAssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest;//从AssetBundleManifest中获取Hash128Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash("");//传递给IsVersionCached函数Caching.IsVersionCached("yourUrl", tempHash128);}}

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);收益率返回 assetBundleCreateRequest;AssetBundle assetBundle = assetBundleCreateRequest.assetBundle;AssetBundleRequest asset = asseBundle.LoadAssetAsync(assetManifestName);收益回报资产;//获取AssetBundleManifestAssetBundleManifest loadedAssetMf = asset.asset as AssetBundleManifest;//从AssetBundleManifest中获取Hash128Hash128 tempHash128 = loadedAssetMf.GetAssetBundleHash("");//传递给IsVersionCached函数Caching.IsVersionCached("yourUrl", tempHash128);}

I want to know AssetBundle already in cache. This is usually done with the Caching.IsVersionCached function.

The Caching.IsVersionCached(string url, int version) function overload is no longer supported in Unity 2017.1.

Unity 2017.3 is recommends that I use the Caching.IsVersionCached(string url, Hash128 hash) overload.

I don't know what the Hash128 is and how to obtain and use it. What is Hash128 used for an how do you obtain it from the AssetBundle?


Thank you for your answer.

But I can't solve my problem.

this is my code

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);
    }
}

}my purpose is when assetbundle already in cache, go ahead and need to download, set on PopUp_YesORNoForAssetBundle.

hash128 need when check assetbundle downloaded or version check.

but in your explain, hash128 only get after assetbundle loaded or saved manifestfile in resource folder previous time.

I want to know how to check assetbunle is in cache or not.

if you suggest the way, I'm really thanks to you.

解决方案

Hash128 represents the hash value of the AssetBundle file that makes it easier to compare AssetBundle file versions when they are downloaded.

There is no example on how to do this from the documentation. Below are three ways to obtain Hash128. Which one to use depends on the condition of where the AssetBundle is located and if you want to download it to check the Hash128 or not . In your case, #1 is likely what your are looking for.

1. Obtain Hash128 during AssetBundle build from the Editor then save to the Resources folder:

When building your AssetBundle with the BuildPipeline.BuildAssetBundles function, this function returns AssetBundleManifest. You can obtain Hash128 by using the AssetBundleManifest.GetAssetBundleHash function. Convert the Hash128 to a string with Hash128.ToString() then save it to the Resources folder so that you can access it during run-time.

Example of an Editor build script that will build AssetBundle and save the Hash128 to the Resources folder:

[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();
}

A simple function to load that Hash128 during run-time:

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);
}

How to use:

//Load Hash128 from the Resources folder
Hash128 tempHash128 = getHash128("AssetInfo/AssetBundleInfo");

//Pass to the IsVersionCached function
Caching.IsVersionCached("yourUrl", tempHash128);

You can also use json to represent and save many as Hash128 as you need.


2. Download the AssetBundle from server then obtain Hash128 during run-time without the Resources folder. Unfortunately, you have to download the AssetBundle first with this method before you can obtain its Hash128. After downloading it, load the data as AssetBundleManifest then obtain Hash128 from it with the AssetBundleManifest.GetAssetBundleHash function. You can then save this Hash128 for later use.

The example below should download and extract Hash128 from an AssetBundle url. No Editor code involved.

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);
    }
}


3. Obtain Hash128 from an AssetBundle in the file system. If you already knew the path of the AssetBundle, load the AssetBundle from that path then load the data as AssetBundleManifest and finally obtain Hash128 from it with the AssetBundleManifest.GetAssetBundleHash function. You can then save this Hash128 for later use.

This shows how to load AssetBundle from the StreamingAsset path and obtain its 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);
}

这篇关于从 AssetBundle 为 Caching.IsVersionCached 函数获取 Hash128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 11:19