Beispiel #1
0
    /// <summary>
    /// 加载AssetBundle打包信息
    /// </summary>
    private void loadAssetBundleBuildInfo()
    {
        // 确保之前加载的AB打包信息卸载彻底
        if (mAssetBundleBuildInfo != null)
        {
            Resources.UnloadAsset(mAssetBundleBuildInfo);
            mAssetBundleBuildInfo = null;
        }
        // AssetBundle打包信息比较特殊,在未加载完成前拿不到AB名字映射
        // 所以这里单独特殊加载,不走正常流程
        var         abpath = AssetBundlePath.GetABLoadFullPath(ResourceConstData.AssetBundleBuildInfoAssetName);
        AssetBundle ab     = null;

        ab = AssetBundle.LoadFromFile(abpath);
        if (ab != null)
        {
            mAssetBundleBuildInfo = ab.LoadAsset <AssetBundleBuildInfoAsset>(ResourceConstData.AssetBundleBuildInfoAssetName);
            mAssetBundleBuildInfo.init();
            ab.Unload(false);
            Debug.Log("AssetBundle打包信息文件加载成功!");
        }
        else
        {
            Debug.LogError($"找不到AssetBundle打包信息文件:{ResourceConstData.AssetBundleBuildInfoAssetName}");
        }
    }
    /// <summary>
    /// AB加载携程
    /// </summary>
    /// <returns></returns>
    private IEnumerator assetBundleLoadAsync()
    {
        while (true)
        {
            if (ABAsyncQueue.Count > 0)
            {
                CurrentLoadingAssetBundleLoader = ABAsyncQueue.Dequeue();
                //检查是否已经同步加载完成
                //如果异步加载AB时,同步请求来了,打断异步后续逻辑
                //LoadState == ResourceLoadState.None表明同步加载该资源已经完成,无需再异步返回
                if (CurrentLoadingAssetBundleLoader.LoadState == ResourceLoadState.None)
                {
                    //ResourceLogger.logWar("有资源还未开始异步加载就被同步加载打断!");
                }
                else
                {
                    CurrentLoadingAssetBundleLoader.LoadState = ResourceLoadState.Loading;
                    var abname = CurrentLoadingAssetBundleLoader.AssetBundleName;
                    var abpath = AssetBundlePath.GetABLoadFullPath(abname);
                    AssetBundleCreateRequest abrequest = null;
#if UNITY_EDITOR
                    //因为资源不全,很多资源丢失,导致直接报错
                    //这里临时先在Editor模式下判定下文件是否存在,避免AssetBundle.LoadFromFileAsync()直接报错
                    if (System.IO.File.Exists(abpath))
                    {
                        abrequest = AssetBundle.LoadFromFileAsync(abpath);
                    }
                    else
                    {
                        Debug.LogError(string.Format("AB : {0}文件不存在!", CurrentLoadingAssetBundleLoader.AssetBundleName));
                    }
#else
                    abrequest = AssetBundle.LoadFromFileAsync(abpath);
#endif
                    yield return(abrequest);

                    //如果异步加载AB时,同步请求来了,打断异步后续逻辑
                    //LoadState == ResourceLoadState.None表明同步加载该资源已经完成,无需再异步返回
                    if (CurrentLoadingAssetBundleLoader.LoadState == ResourceLoadState.None)
                    {
                        ResourceLogger.log(string.Format("资源 : {0}加载已完成,异步加载被打断!", abname));
                    }
                    else
                    {
                        var assetbundle = abrequest.assetBundle;
                        if (assetbundle == null)
                        {
                            ResourceLogger.logErr(string.Format("Failed to load AssetBundle : {0}!", CurrentLoadingAssetBundleLoader.AssetBundleName));
                        }
                        CurrentLoadingAssetBundleLoader.onSelfABLoadComplete(assetbundle);
                    }
                }
                CurrentLoadingAssetBundleLoader = null;
            }
            else
            {
                yield return(null);
            }
        }
    }
    /// <summary>
    /// 同步加载AB
    /// </summary>
    /// <returns></returns>
    private void loadAssetBundleSync()
    {
        var         abpath = AssetBundlePath.GetABLoadFullPath(AssetBundleName);
        AssetBundle ab     = null;

#if UNITY_EDITOR
        //因为资源不全,很多资源丢失,导致直接报错
        //这里临时先在Editor模式下判定下文件是否存在,避免AssetBundle.LoadFromFile()直接报错
        if (System.IO.File.Exists(abpath))
        {
            ab = AssetBundle.LoadFromFile(abpath);
        }
        else
        {
            Debug.LogError(string.Format("AB : {0}文件不存在!", AssetBundleName));
        }
#else
        ab = AssetBundle.LoadFromFile(abpath);
#endif
        onSelfABLoadComplete(ab);
    }