AssetBundleManager中的Update方法,是程序前进的推动者,因为update方法是逐帧执行,时刻的都在观察执行
void Update()
{
var keysToRemove = new List<string>();
//检查所有正在下载的WWW,是否已经下载完毕
foreach (var keyValue in m_DownloadingWWWs)
{
WWW download = keyValue.Value;
if (download.error != null)
{
m_DownloadingErrors.Add(keyValue.Key, download.error);
keysToRemove.Add(keyValue.Key);
continue;
}
// 如下载完成,进行封装保存到字典中
if(download.isDone)
{
//Debug.Log("Downloading " + keyValue.Key + " is done at frame " + Time.frameCount);
m_LoadedAssetBundles.Add(keyValue.Key, new LoadedAssetBundle(download.assetBundle) );
keysToRemove.Add(keyValue.Key);
}
}
// 从字典中移除已经下载完成的WWW,并且对WWW进行释放
foreach( var key in keysToRemove)
{
WWW download = m_DownloadingWWWs[key];
m_DownloadingWWWs.Remove(key);
download.Dispose();
}
// 检查是否可以从AB包中下载资源
for (int i=0;i<m_InProgressOperations.Count;)
{
if (!m_InProgressOperations[i].Update())
{
//处理完就移掉
m_InProgressOperations.RemoveAt(i);
}
else
{
//当前暂不处理,接着处理下一个
i++;
}
}
}
上面代码中这行代码m_InProgressOperations[i].Update()
其中Update 里面代码: 就是看 AB包是不是已经下载完成,如果下载完成,就开始异步加载AB包中的资源(asset)
public class AssetBundleLoadAssetOperationFull : AssetBundleLoadAssetOperation
{
protected string m_AssetBundleName;
protected string m_AssetName;
protected string m_DownloadingError;
protected System.Type m_Type;
protected AssetBundleRequest m_Request = null;
public AssetBundleLoadAssetOperationFull (string bundleName, string assetName, System.Type type)
{
m_AssetBundleName = bundleName;
m_AssetName = assetName;
m_Type = type;
}
public override T GetAsset<T>()
{
if (m_Request != null && m_Request.isDone)
return m_Request.asset as T;
else
return null;
}
public override bool Update ()
{
if (m_Request != null)
return false;
LoadedAssetBundle bundle = AssetBundleManager.GetLoadedAssetBundle (m_AssetBundleName, out m_DownloadingError);
if (bundle != null)
{
m_Request = bundle.m_AssetBundle.LoadAssetAsync (m_AssetName, m_Type);
return false;
}
else
{
Debug.Log("说明还在下AssetBundle");
return true;
}
}
public override bool IsDone ()
{
if (m_Request == null && m_DownloadingError != null)
{
Debug.LogError(m_DownloadingError);
return true;
}
return m_Request != null && m_Request.isDone;
}
}
总体思想:
WWW下载,并将WWW对象保存字典
WWW下载完成,取出Assetbundle,封装到LoadedAssetBundle对象中,将封装对象保存字典中
开始异步加载AseetBundle中的asset
记载完成后,开始实例化
FR:海涛高软(QQ技术交流群:386476712)