示例#1
0
        /// <summary>
        /// 对于正在加载的请求,根据加载的情况更新其进度或者完成资源的加载
        /// 更新加载状态,进度等,并反馈此次加载任务的完成结果
        /// </summary>
        /// <param name="loaderData">加载任务数据</param>
        /// <returns></returns>
        protected override bool UpdateLoadingLoaderData(AssetLoaderData loaderData)
        {
            AssetLoaderHandle loaderHandle = null;

            if (m_LoaderHandleDic.ContainsKey(loaderData.m_UniqueID))//如果查找不到表示此次资源加载操作被取消了
            {
                loaderHandle = m_LoaderHandleDic[loaderData.m_UniqueID];
            }

            bool isComplete = true;   //加载是否完成

            for (int i = 0; i < loaderData.m_AssetPaths.Length; ++i)
            {
                if (loaderData.GetLoadState(i))//加载完成了跳过
                {
                    continue;
                }
                string    assetPath = loaderData.m_AssetPaths[i];
                AssetNode assetNode = m_AssetNodeDic[assetPath];
                if (assetNode == null)
                {
                    //assetNode 存储值为空,直接此assetNode当做完成,为上面GetLoadState 跳过过滤
                    loaderData.SetLoadState(i);
                    loaderData.InvokeComplete(i, null);
                    continue;
                }

                if (loaderHandle == null)
                {
                    //loaderHandle 值为空,assetNode完成,为上面GetLoadState 跳过过滤
                    if (assetNode.IsDone)
                    {
                        assetNode.ReleaseLoadCount();
                        loaderData.SetLoadState(i);
                    }
                    else
                    {
                        isComplete = false;
                    }
                    continue;
                }


                if (assetNode.IsDone)             //判断是否完成,如果完成了加载则进行相关的回调
                {
                    assetNode.ReleaseLoadCount(); //计数-1
                    UnityObject uObj = null;
                    if (loaderData.m_IsInstance)
                    {
                        uObj = assetNode.GetInstance();
                    }
                    else
                    {
                        uObj = assetNode.GetAsset();
                    }

                    if (uObj == null)
                    {
                        Debug.LogError($"AssetBundleLoader::AssetLoadComplete->加载成功,但Obj 是空,path = {assetPath}");
                    }

                    //保存Obj ,进度,状态,执行单资源回调
                    loaderHandle.SetObject(i, uObj);
                    loaderHandle.SetProgress(i, 1.0f);

                    loaderData.SetLoadState(i);
                    loaderData.InvokeComplete(i, uObj);
                }
                else
                {
                    //加载中,未完成, 跟新进度
                    float progress    = GetAssetLoadingProgress(assetPath);
                    float oldProgress = loaderHandle.GetProgress(i);
                    if (oldProgress != progress)
                    {
                        loaderHandle.SetProgress(i, progress);
                        loaderData.InvokeProgress(i, progress);
                    }
                    isComplete = false;
                }
            }
            if (loaderHandle != null)
            {
                //更新全部进度
                loaderData.InvokeBatchProgress(loaderHandle.AssetProgresses);

                //全部完成
                if (isComplete)
                {
                    loaderHandle.State = AssetLoaderState.Complete;
                    loaderData.InvokeBatchComplete(loaderHandle.AssetObjects);
                }
            }

            return(isComplete);
        }
        /// <summary>
        /// 更新加载状态,进度等,并反馈此次加载任务的完成结果
        /// </summary>
        /// <param name="loaderData">加载任务数据</param>
        /// <returns></returns>
        protected override bool UpdateLoadingLoaderData(AssetLoaderData loaderData)
        {
            List <AssetDatabaseAsyncOperation> operationList = m_AsyncOperationDic[loaderData.m_UniqueID];
            bool isComplete = true;

            AssetLoaderHandle loaderHandle = null;

            if (m_LoaderHandleDic.ContainsKey(loaderData.m_UniqueID))
            {
                loaderHandle = m_LoaderHandleDic[loaderData.m_UniqueID];
            }

            for (int i = 0; i < loaderData.m_AssetPaths.Length; ++i)
            {
                if (loaderData.GetLoadState(i)) //加载完成了跳过
                {
                    continue;
                }
                string assetPath = loaderData.m_AssetPaths[i];
                AssetDatabaseAsyncOperation operation = operationList[i];

                if (operation.Status == AssetAsyncOperationStatus.Loaded) //操作状态为完成了
                {
                    UnityObject uObj = operation.GetAsset();

                    if (uObj == null)
                    {
                        //Debug.LogError($"AssetDatabaseLoader::UpdateLoadingLoaderData->asset is null.path = {assetPath}");
                        Debug.LogError($"加载完成,但资源UnityObject == null,   path = {assetPath}");
                    }

                    //实例化
                    if (uObj != null && loaderData.m_IsInstance)
                    {
                        uObj = UnityObject.Instantiate(uObj);
                    }

                    //保存Obj ,进度,状态,执行单资源回调
                    loaderHandle.SetObject(i, uObj);
                    loaderHandle.SetProgress(i, 1.0f);

                    loaderData.SetLoadState(i);
                    loaderData.InvokeComplete(i, uObj);
                }
                else if (operation.Status == AssetAsyncOperationStatus.Loading)   //加载中,未完成
                {
                    //跟新进度
                    float oldProgress = loaderHandle.GetProgress(i);
                    float curProgress = operation.Progress();
                    if (oldProgress != curProgress)
                    {
                        loaderHandle.SetProgress(i, curProgress);
                        loaderData.InvokeProgress(i, curProgress);
                    }
                    isComplete = false;
                }
                else
                {
                    isComplete = false;
                }
            }
            //更新全部进度
            loaderData.InvokeBatchProgress(loaderHandle.AssetProgresses);

            //全部完成
            if (isComplete)
            {
                loaderHandle.State = AssetLoaderState.Complete;
                loaderData.InvokeBatchComplete(loaderHandle.AssetObjects);
                m_AsyncOperationDic.Remove(loaderData.m_UniqueID);
            }
            return(isComplete);
        }