Unity ab包(assetbundle加载)-ui和预设体以及场景
作者:互联网
protected Dictionary<string, object> m_bundles = new Dictionary<string, object>();
//----------------------------------------------------------------加载UI或预设体--------------------------------------------------------------------------------------------------
第一步:加载ab包
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(_uri + it);
//request.Send();
request.SendWebRequest();
while (!request.isDone)
{
m_prograss_txt = "正在加载\"" + str_names[0] + "\"资源包";
m_prograss = (int)(request.downloadProgress * 100);
yield return 1;
}
if (request.isDone)
{
m_prograss = (int)(request.downloadProgress * 100);
m_prograss_color -= new Color(0.33f, 0.0f, 0.33f, 0.0f);
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
print(str_names[0] + "保存到字典");
m_bundles.Add(str_names[0], ab);
}
第二步:加载资源到场景
if (m_bundles.ContainsKey(_ab_key) && m_bundles[_ab_key] != null)
{
AssetBundle ab = m_bundles[_ab_key] as AssetBundle;
if (ab != null)
{
GameObject obj = Instantiate<GameObject>(ab.LoadAsset(_key) ) as GameObject;
}
}
//----------------------------------------------------------------加载场景--------------------------------------------------------------------------------------------------
第一步:加载场景的ab包
protected Dictionary<string, object> m_bundles = new Dictionary<string, object>();
UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(_uri + it);
//request.Send();
request.SendWebRequest();
while (!request.isDone)
{
m_prograss_txt = "正在加载\"" + str_names[0] + "\"资源包";
m_prograss = (int)(request.downloadProgress * 100);
yield return 1;
}
if (request.isDone)
{
m_prograss = (int)(request.downloadProgress * 100);
m_prograss_color -= new Color(0.33f, 0.0f, 0.33f, 0.0f);
AssetBundle ab = (request.downloadHandler as DownloadHandlerAssetBundle).assetBundle;
print(str_names[0] + "保存到字典");
m_bundles.Add(str_names[0], ab);
}
第二步:跳转场景
SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.LoadScene(this.getLoadSceneName());
第三步:加载完成,卸载场景
public void OnSceneLoaded()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
foreach (var it in _ab_key)
{
if (m_bundles.ContainsKey(it))
{
AssetBundle ab = m_bundles[it] as AssetBundle;
if (ab != null)
{
ab.Unload(false);
}
m_bundles.Remove(it);
return;
}
}
}
//------------------------------------------------------------------加载配置文件ab包------------------------------------------------------------------------------
if (str_names[1] == "xml" || str_names[1] =="cfg" || str_names[1] == "txt")
{
UnityWebRequest request = UnityWebRequest.Get(_uri + it);
request.SendWebRequest();
while (!request.isDone)
{
m_prograss_txt = "正在加载\"" + str_names[0] + "\"资源包";
m_prograss = (int)(request.downloadProgress * 100);
yield return 1;
}
if (request.isDone)
{
m_prograss = (int)(request.downloadProgress * 100);
m_prograss_color -= new Color(0.33f, 0.0f, 0.33f, 0.0f);
string data = request.downloadHandler.text;
print(data);
m_bundles.Add(str_names[0], data);
}
}
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712
标签:ab,prograss,bundles,request,Unity,assetbundle,names,str 来源: https://blog.csdn.net/qq_15267341/article/details/121476245