首页 文章

从AssetBundle获取Hash128以获取Caching.IsVersionCached函数

提问于
浏览
2

我想知道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 <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);
    }
}

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

检查assetbundle下载或版本检查时需要hash128 .

但是在你的解释中,hash128只能在上次加载资源包或在资源文件夹中保存manifestfile之后才能获得 .

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

如果你建议的话,我真的很感谢你 .

1 回答

  • 3

    但我不知道hash128是什么

    Hash128表示AssetBundle文件的哈希值,可以在下载时更轻松地比较AssetBundle文件版本 .

    我找不到如何使用hash128

    没有关于如何从文档中执行此操作的示例 . 以下是 three 获取Hash128的方法 . 使用哪一个取决于 AssetBundle 所在的条件以及是否要下载它以检查Hash128 . 在您的情况下, #1 很可能是您正在寻找的 .

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

    使用BuildPipeline.BuildAssetBundles函数构建AssetBundle时,此函数返回AssetBundleManifest . 您可以使用AssetBundleManifest.GetAssetBundleHash函数获取Hash128 . 将Hash128转换为带有Hash128.ToString()的字符串,然后将其保存到Resources文件夹,以便在运行时访问它 .

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

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

    在运行时加载 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);
    }
    

    如何使用:

    //Load Hash128 from the Resources folder
    Hash128 tempHash128 = getHash128("AssetInfo/AssetBundleInfo");
    
    //Pass to the IsVersionCached function 
    Caching.IsVersionCached("yourUrl", tempHash128);
    

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


    2 . Download the AssetBundle from server then obtain Hash128 during run-time without the Resources folder . 不幸的是,您必须首先使用此方法下载AssetBundle,然后才能获得 Hash128 . 下载后,将数据加载为 AssetBundleManifest ,然后使用 AssetBundleManifest.GetAssetBundleHash 函数从中获取 Hash128 . 然后,您可以保存此 Hash128 供以后使用 .

    下面的示例应从AssetBundle URL下载并解压缩 Hash128 . 没有涉及编辑器代码 .

    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. 如果您已经是knew AssetBundle的路径,请从该路径加载AssetBundle,然后将数据加载为 AssetBundleManifest ,最后使用 AssetBundleManifest.GetAssetBundleHash 函数从中获取 Hash128 . 然后,您可以保存此 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);
    }
    

相关问题