コード例 #1
0
    /// <summary>
    /// 检测自己及依赖是否全部加载完成
    /// </summary>
    /// <param name="bundleName"></param>
    /// <returns></returns>
    private static bool RecursiveCheckLoad(string bundleName)
    {
        if (!ReferenceBundle.IsAssetBundleCraete(bundleName))
        {
            return(false);
        }

        List <string> list = BundleDependencies.GetAssetBundleDependencies(bundleName);

        if (list != null)
        {
            foreach (string tBundleName in list)
            {
                if (!ReferenceBundle.IsAssetBundleCraete(tBundleName))
                {
                    return(false);
                }
                else
                {
                    // 当前已加载 再次向下查找
                    RecursiveCheckLoad(tBundleName);
                }
            }
        }

        return(true);
    }
    /// <summary>
    /// 异步加载资源
    /// </summary>
    /// <param name="assetName">需要加载的资源路径</param>
    /// <param name="onLoadAsset">加载成功之后的回调</param>
    /// <returns>请求Id</returns>
    public static int LoadAssetAsync(string assetName, OnLoadAsset onLoadAsset)
    {
        // 回调不存在不允加载
        if (onLoadAsset == null)
        {
            GameLogger.LogError("[资源加载错误] 加载函数回调不能为空,加载后也没有引用");
            return(0);
        }

        // 资源对应bundle名
        string abName = BundleAsset.GetBundleName(assetName);

        if (abName == null)
        {
            GameLogger.LogError("[资源加载错误] 找不到资源 ", assetName);
            return(0);
        }

        // 获取请求ID
        int requestId = ResourceManager.GetRequestId();

        LoadAssetInfo loadAssetInfo = GetTempLoadAssetInfo();

        loadAssetInfo.requestId   = requestId;
        loadAssetInfo.assetName   = assetName;
        loadAssetInfo.onLoadAsset = onLoadAsset;
        loadAssetInfo.bundleName  = abName;
        EnterLoadWait(loadAssetInfo);

        // 1 asset是否已加载
        if (ReferenceObject.IsObjectCreate(loadAssetInfo.assetName))
        {
            EnterWaitCall(loadAssetInfo);
        }

        // 2 bundle是否已加载
        else if (!ReferenceBundle.IsAssetBundleCraete(abName))
        {
            PushPending(loadAssetInfo);
        }

        // 3 从bundle加载资源
        else
        {
            List <LoadAssetInfo> loadingAssetList = GetLoadAssetStateList(LoadAssetStep.Loading);
            if (loadingAssetList.Count < _frameLoadMaxCount)
            {
                LoadAssetAsync(loadAssetInfo);
            }
        }

        _loadAssetInfoList.Add(loadAssetInfo);

        return(requestId);
    }