从AssetBundle获取Hash128以获取Caching.IsVersionCached函数 [英] Get Hash128 from AssetBundle for the Caching.IsVersionCached function

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

问题描述

我想知道AssetBundle已在缓存中.通常使用Caching.IsVersionCached函数完成此操作.

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

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

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

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

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

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?

谢谢您的回答.

但是我不能解决我的问题.

But I can't solve my problem.

这是我的代码

表示(int i = 0; i

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上进行设置.

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

在检查资产捆绑下载或版本检查时需要hash128.

hash128 need when check assetbundle downloaded or version check.

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

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

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

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

but I don't know what is hash128

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

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

我找不到如何使用hash128

I can't find how to use hash128

文档中没有有关如何执行此操作的示例.以下是三种方法来获取 Hash128 .使用哪一个取决于AssetBundle所在的条件,以及是否要下载它以检查

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 . 在AssetBundle构建期间从编辑器中获取Hash128,然后保存到资源文件夹中:

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

在使用 AssetBundle 时docs.unity3d.com/ScriptReference/BuildPipeline.BuildAssetBundles.html"rel =" nofollow noreferrer> BuildPipeline.BuildAssetBundles 函数,此函数返回 Hash128 . docs.unity3d.com/ScriptReference/AssetBundleManifest.GetAssetBundleHash.html"rel =" nofollow noreferrer> AssetBundleManifest.GetAssetBundleHash 函数.将 Hash128 转换为带有

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.

编辑器构建脚本的示例,该脚本将构建AssetBundle并将Hash128保存到Resources文件夹:

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

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

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

使用方法:

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

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

您还可以根据需要使用json表示并另存为Hash128.

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

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

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.

以下示例应从AssetBundle网址下载并提取Hash128.不涉及编辑器代码.

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 . 从文件系统中的AssetBundle获取Hash128.如果您已经


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.

这显示了如何从StreamingAsset路径加载AssetBundle并获取其Hash128:

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获取Hash128以获取Caching.IsVersionCached函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆