Ejemplo n.º 1
0
    /// <summary>
    /// 同步加载AssetBundle
    /// </summary>
    /// <param name="bundleNames">需要加载的bundle集合</param>
    /// <param name="groupName">缓存AssetBundle的场景。groupName不为空,则缓存AssetBundle。用于离开场景时销毁AssetBundle</param>
    /// <returns></returns>
    public Dictionary <string, AssetBundle> LoadAssetBundle(string[] bundleNames, string groupName = "")
    {
        //本次加载的AssetBundle集合
        Dictionary <string, AssetBundle> loadedAssetBundleDic = new Dictionary <string, AssetBundle>();

        foreach (var bundleName in bundleNames)
        {
            MyCachedAssetBundle cab = LoadSingle(bundleName, groupName);

            if (cab == null)
            {
                continue;
            }

            loadedAssetBundleDic.Add(bundleName, cab.AssetBundle);
        }
        return(loadedAssetBundleDic);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// 同步加载单个AssetBundle
    /// </summary>
    /// <param name="bundleName"></param>
    /// <param name="groupName"></param>
    /// <returns></returns>
    private MyCachedAssetBundle LoadSingle(string bundleName, string groupName)
    {
        LoadBundleInfo loadBundleInfo;

        if (!_bundleInfoDic.TryGetValue(bundleName, out loadBundleInfo))
        {
            LogUtil.LogError($"加载AssetBundle失败,AssetBundle“{bundleName}”不存在");
            return(null);
        }

        MyCachedAssetBundle temp;

        if (_cachedAssetBundleDic.TryGetValue(bundleName, out temp))
        {
            if (string.IsNullOrEmpty(groupName))
            {
                temp.AddGroup(groupName);
            }
            return(temp);
        }

        Debug.Log("loadBundleInfo = " + loadBundleInfo);

        LoadDependecies(bundleName, groupName);

        AssetBundle ab;

        //从streamingAssetsPath加载使用“AssetBundle.LoadFromFile”方法,同步加载更快。
        if (loadBundleInfo.LoadUrl.Contains(Application.streamingAssetsPath))
        {
            ab = AssetBundle.LoadFromFile(loadBundleInfo.LoadUrl);
        }
        //否则加载使用“UnityWebRequest.GetAssetBundle”方法,此方法会自动从缓存加载数据,
        //若缓存下没有数据,则会从服务器下载数据,并缓存到本地。
        else
        {
            UnityWebRequest request = UnityWebRequest.GetAssetBundle(loadBundleInfo.LoadUrl, Hash128.Parse(loadBundleInfo.BundleInfo.bundleHash128), 0);
            request.SendWebRequest();

            while (!request.isDone)
            {
                Debug.Log(string.Format("Load AssetBundle :{0},progress = {1}", loadBundleInfo.BundleInfo.bundleName, request.downloadProgress));
            }

            if (request.error != null)
            {
                Debug.LogError($"加载AssetBundle失败,url:{loadBundleInfo.LoadUrl},error:{request.error}");
                request.Dispose();
                return(null);
            }

            ab = DownloadHandlerAssetBundle.GetContent(request);
            request.Dispose();
        }

        MyCachedAssetBundle cab = new MyCachedAssetBundle(ab);

        if (!string.IsNullOrEmpty(groupName))
        {
            cab.AddGroup(groupName);
            _cachedAssetBundleDic.Add(bundleName, cab);
        }
        return(cab);
    }