收藏
回答

Unity微信小游戏 加载远程AB包出现构建平台错误的报错问题?

团结引擎 1.2.3

打包工具,使用 AssetBundles-Browser-1.7.0

打包内容,本地和UOS

Unity远程加载代码

    public class AssetBundlesManager : SingletonMonoBehaviour<AssetBundlesManager>
    {
        // AB包不能够重复加载 重复加载会报错
        // 字典 用字典来存储 加载过的AB包
        private readonly Dictionary<string, AssetBundle> _abDic = new();

        /// <summary>
        ///     状态锁
        /// </summary>
        private readonly HashSet<string> _loadingAbNames = new();

        // AB包管理器 目的是
        // 让外部更方便的进行资源加载

        // 主包
        private AssetBundle _mainAb;

        // 依赖包获取用的配置文件
        private AssetBundleManifest _manifest;

        /// <summary>
        ///     这个AB包存放路径 方便修改
        /// </summary>
        private static string PathUrl => Application.streamingAssetsPath + "/";

        /// <summary>
        ///     主包名 方便修改
        /// </summary>
        private static string MainAbName
        {
            get
            {
#if UNITY_IOS
                return "IOS";
#elif UNITY_ANDROID
                return "Android";
#elif UNITY_WEBGL
                return "WebGL";
#else
                return "PC";
#endif
            }
        }

        /// <summary>
        ///     生成字典 abName -> abNameWithHash
        /// </summary>
        /// <param name="abm"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetAbNamesWithHash(AssetBundleManifest abm)
        {
            var hashNames = abm.GetAllAssetBundles();
            var abNamesDict = new Dictionary<string, string>();
            foreach (var hashName in hashNames)
            {
                // 需要注意AB后缀名,默认 .unity3d
                var abName = GetSubstring(hashName);
                abNamesDict.Add(abName, hashName);
            }

            return abNamesDict;
        }

        private static string GetSubstring(string hashName)
        {
            return Regex.Match(hashName,
                "_[0-9a-f]{32}$").Success
                ? hashName.Substring(0, hashName.Length - 33)
                : hashName;
        }  

        /// <summary>
        ///     UnityWebRequest加载依赖
        /// </summary>
        /// <param name="abName"></param>
        /// <returns></returns>
        private IEnumerator LoadDependencies(string abName)
        {
            // 标记为正在加载
            while (!_loadingAbNames.Add(abName))
            {
                yield return null;
            }

            // 加载主包
            if (_mainAb is null)
            {
                var abRequest = UnityWebRequestAssetBundle.GetAssetBundle(PathUrl + MainAbName);
                yield return abRequest.SendWebRequest();
                if (abRequest.result != UnityWebRequest.Result.Success)
                {
                    Debug.Log(abRequest.error);
                    yield break;
                }

                _mainAb = DownloadHandlerAssetBundle.GetContent(abRequest);
                // 获取主包下的 AssetBundleManifest 资源文件(存有依赖信息)
                _manifest = _mainAb.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
            }

            var hashNames = GetAbNamesWithHash(_manifest);
            //获取ab带hash的完整文件名
            var abNameWithHash = hashNames[abName];

            // 加载依赖
            AssetBundle ab;
            var dependencies = _manifest.GetAllDependencies(abNameWithHash);
            foreach (var key in dependencies)
            {
                var keyName = GetSubstring(key);

                // 判断包是否加载过
                if (!_abDic.ContainsKey(keyName))
                {
                    var depRequest = UnityWebRequestAssetBundle.GetAssetBundle(PathUrl + key);
                    yield return depRequest.SendWebRequest();
                    if (depRequest.result != UnityWebRequest.Result.Success)
                    {
                        Debug.LogError(GetType() + "/ERROR/" + depRequest.error);
                    }
                    else
                    {
                        ab = (depRequest.downloadHandler as DownloadHandlerAssetBundle)?.assetBundle;
                        _abDic.Add(keyName, ab);
                    }

                    depRequest.Dispose();
                }
            }

            // 加载资源来源包
            // 如果没有加载过 再加载
            if (!_abDic.ContainsKey(abName))
            {
                var depRequest = UnityWebRequestAssetBundle.GetAssetBundle(PathUrl + abNameWithHash);
                yield return depRequest.SendWebRequest();
                ab = DownloadHandlerAssetBundle.GetContent(depRequest);
                _abDic.Add(abName, ab);
            }

            _loadingAbNames.Remove(abName); // 加载完成后移除标记
        }

        /// <summary>
        ///     UnityWebRequest加载
        /// </summary>
        /// <param name="abName"></param>
        /// <param name="resName"></param>
        /// <param name="callBack"></param>
        /// <typeparam name="T"></typeparam>
        public void LoadByRequest<T>(string abName, string resName, UnityAction<T> callBack) where T : Object
        {
            StartCoroutine(ReallyLoadByRequest(abName, resName, callBack));
        }

        private IEnumerator ReallyLoadByRequest<T>(string abName, string resName, UnityAction<T> callBack)
            where T : Object
        {
            yield return StartCoroutine(LoadDependencies(abName));
            if (!_abDic.ContainsKey(abName))
            {
                var hashNames = GetAbNamesWithHash(_manifest);
                //获取ab带hash的完整文件名
                var abNameWithHash = hashNames[abName];

                var request = UnityWebRequestAssetBundle.GetAssetBundle(PathUrl + abNameWithHash);

                yield return request.SendWebRequest();
                if (request.result != UnityWebRequest.Result.Success)
                {
                    Debug.Log("Request.error");
                }
                else
                {
                    var ab = DownloadHandlerAssetBundle.GetContent(request);
                    _abDic[abName] = ab;
                }

                request.Dispose();
            }

            var loadedResource = _abDic[abName].LoadAsset<T>(resName);
            callBack(loadedResource);
        }
}


报错信息

Unity内正常使用,打包出来微信开发工具就失败

回答关注问题邀请回答
收藏
登录 后发表内容