示例#1
0
    private static void BuildPrefabWhenInAsyncLoading(ref ResNode res, UnionResConfigData config)
    {
        //先检查
        if (null == res)
        {
            return;
        }

        if (!isOnRunTimeMode)
        {
            return;
        }

        if (!res.isAsync)
        {
            return;
        }

        if (res.isLoadFinish)
        {
            return;
        }

        res.StartLoadedTime();
        //先从回调列表里移除
        if (m_AsyncLoadingDependicensPrefabResNode.ContainsKey(res.instanceID))
        {
            m_AsyncLoadingDependicensPrefabResNode.Remove(res.instanceID);
        }
        //卸载依赖
        UnLoadDependenicesAsync(res.instanceID);

        //开始加载依赖
        if (LoadDependenices(config))
        {
            ResourcePackage pkg = null;

            pkg = GetResourcePackge(config);

            if (null != pkg)
            {
                LoadAssetFromAssetBundle <GameObject>(ref res, config, pkg.AssetBundleInstance);
            }
            else
            {
                AssetBudleLog("加载AssetBundle失败:" + config.AssetBundleName);
            }

            if (!res.IsValid())
            {
                UnLoadDependenices(res.instanceID);
            }
        }
        else
        {
            UnLoadDependenices(res.instanceID);
        }

        res.status = ResNodeStatus.LoadFinish;
        m_PrefabResTable.Add(res.instanceID, res);

        //取出回调列表
        List <AsyncLoadingObject> list = null;

        if (m_PrefabRequestTable.TryGetValue(res.instanceID, out list))
        {
            ReleaseDependenicesAssetBundleIfNeed(res);
            ReleaseAssetBundleIfNeed(res);
            m_PrefabRequestTable.Remove(res.instanceID);

            foreach (AsyncLoadingObject obj in list)
            {
                PushToResDelayCallBackList(res, config, obj.callBack, obj.UserDataObj);
            }
        }


        return;
    }
    /// <summary>
    /// 异步构建package
    /// </summary>
    /// <param name="reslutTable"></param>
    /// <param name="config"></param>
    /// <param name="resourcePackageCallBack"></param>
    /// <param name="UserCallBack"></param>
    /// <param name="userDataObj"></param>
    /// <returns></returns>
    private static void BuildPacakgeAsync(ref ResNode result, Dictionary <int, ResNode> reslutTable, ResConfigData config, System.Action <ResourcePackageCallBackObj> resourcePackageCallBack,
                                          System.Action <ResConfigData, ResNode, System.Object> UserCallBack, System.Object userDataObj, bool isPrefabResNode = false)
    {
        string patch = AssetBundleManager.FullAssetPackagePathRoot + config.AssetBundleName;

        UnionResConfigData unionRes = config as UnionResConfigData;
        string             abPath   = config.AssetBundleName;

        if (unionRes != null)
        {
            patch  = unionRes.UnionFilePath;
            abPath = unionRes.ShortUnionFilePath;
        }
        if (!m_RespackageTable.ContainsKey(abPath))
        {
            AssetBudleLog("AssetBundle文件不存在!,路径:" + patch);
            return;
        }

        result = AllocResNode(config.AssetGUID);

        result.StartLoadedTime();

        //预制体是预先创建好ResNode的,所以不能清除和增加引用,直接使用外部的即可
        if (!isPrefabResNode)
        {
            result.Clear();
            result.AddRef();
        }

        result.config  = config;
        result.isAsync = true;
        result.status  = ResNodeStatus.WatingForLoad;

        AssetRequest are = null;

        are = AllocAssetRequest(result.instanceID);
        m_LoadingAssetList.Add(result.instanceID);
        are.Clear();

        reslutTable.Add(result.instanceID, result);

        GenerateResCallBack(result, config, UserCallBack, userDataObj);

        ResourcePackage pakg = AllocResourcePackage(config.AssetBundleName);

        pakg.priority = config.priority;
        //已经有了
        if (m_LoadedResourcePackage.Contains(pakg.instanceID))
        {
            pakg.AddRef();
            pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj));

            PushToResourcePackageCallBackList(pakg.instanceID);
        }
        else
        {
            //如果卸载列表里,先移除掉,避免加载出来就被卸载了
            RemoveNeedToUnLoadPackage(pakg.instanceID);
            //没有开启异步加载
            if (!enabledAsyncLoadAssetFile)
            {
                pakg = GetResourcePackge(config);
                pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj));
                PushToResourcePackageCallBackList(pakg.instanceID);
                return;
            }

            //全新的包
            if (!m_ResourcePackageRequestTable.Contains(pakg.instanceID))
            {
                pakg.Clear();
                pakg.isAsync   = true;
                pakg.filePatch = patch;

                FillUnioFileInfo(config as UnionResConfigData, pakg);
                EnQueueWaitForUpLoadAssetsFileAsyncRequestTable(pakg.instanceID);
                m_ResourcePackageRequestTable.Add(pakg.instanceID);
            }

            pakg.AddAsyncCallBackData(resourcePackageCallBack, BuildResourcePackageCallBackObj(pakg, result, config, UserCallBack, userDataObj));
            pakg.AddRef();
        }
        return;
    }