gpt4 book ai didi

c# - 从 AssetBundle 获取 Hash128 用于 Caching.IsVersionCached 函数

转载 作者:行者123 更新时间:2023-11-30 20:27:51 25 4
gpt4 key购买 nike

我想知道 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 上设置。

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

但在您的解释中,hash128 仅在 assetbundle 加载或上次资源文件夹中保存 list 文件后获取。

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

如果你提出了方法,我真的很感谢你。

最佳答案

but I don't know what is hash128

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

I can't find how to use hash128

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

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

构建您的 AssetBundle 时与 BuildPipeline.BuildAssetBundles函数,此函数返回 AssetBundleManifest .您可以获得Hash128通过使用 AssetBundleManifest.GetAssetBundleHash功能。转换 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从服务器下载 AssetBundle,然后在运行时获取 Hash128,无需 Resources 文件夹。不幸的是,您必须先使用此方法下载 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从文件系统中的AssetBundle中获取Hash128如果你已经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);
}

关于c# - 从 AssetBundle 获取 Hash128 用于 Caching.IsVersionCached 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com