Exemplo n.º 1
0
        /// <summary>
        /// 设置加载操作数据,创建 Operation
        /// </summary>
        /// <param name="loaderData">加载任务数据</param>

        protected override void StartLoaderDataLoading(AssetLoaderData loaderData)
        {
            for (int i = 0; i < loaderData.m_AssetPaths.Length; ++i)
            {
                string assetPath = loaderData.m_AssetPaths[i];

                if (m_AssetNodeDic.TryGetValue(assetPath, out AssetNode assetNode))
                {
                    //已经有过了,计数+1
                    assetNode.RetainLoadCount();
                    continue;
                }

                string mainBundlePath = m_AssetAddressConfig.GetBundlePathByPath(assetPath);
                if (!m_BundleNodeDic.TryGetValue(mainBundlePath, out BundleNode bundleNode))
                {
                    bundleNode = CreateBundleNode(mainBundlePath);
                }
                //对应一个AssetNode
                assetNode = m_AssetNodePool.Get();
                assetNode.InitNode(assetPath, bundleNode);
                assetNode.RetainLoadCount();                                                                            //计数  = 1

                m_AssetNodeDic.Add(assetPath, assetNode);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 设置加载操作数据,创建 Operation
        /// </summary>
        /// <param name="loaderData">加载任务数据</param>
        protected override void StartLoaderDataLoading(AssetLoaderData loaderData)
        {
            List <AssetDatabaseAsyncOperation> operationList = new List <AssetDatabaseAsyncOperation>();

            m_AsyncOperationDic.Add(loaderData.m_UniqueID, operationList);
            for (int i = 0; i < loaderData.m_AssetPaths.Length; ++i)
            {
                AssetDatabaseAsyncOperation operation = new AssetDatabaseAsyncOperation(loaderData.m_AssetPaths[i]);
                m_LoadingAsyncOperationList.Add(operation);
                operationList.Add(operation);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 停止指定资源的加载
        /// </summary>
        /// <param name="loaderData"></param>

        protected override void UnloadLoadingAssetLoader(AssetLoaderData loaderData)
        {
            List <AssetDatabaseAsyncOperation> operationList = m_AsyncOperationDic[loaderData.m_UniqueID];

            operationList.ForEach((operation) =>
            {
                m_LoadingAsyncOperationList.Remove(operation);//全局加载实施操作列表 ,移除本次加载任务的 所有操作Operation
            });
            m_AsyncOperationDic.Remove(loaderData.m_UniqueID);

            m_LoaderDataLoadingList.Remove(loaderData);
            m_LoaderDataPool.Release(loaderData);
        }
Exemplo n.º 4
0
 /// <summary>
 ///  卸载,加载中“加载数据”执行删除
 /// </summary>
 /// <param name="loaderData"></param>
 protected override void UnloadLoadingAssetLoader(AssetLoaderData loaderData)
 {
 }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        /// <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);
        }