已删除用户
发布于 2023-10-30 / 1 阅读 / 0 评论 / 0 点赞

解决Webgl平台不支持.net的IO流问题

解决Webgl平台不支持.net的IO流问题

经过真机测试以及查阅资料得知Webgl平台不支持.net的IO流

解决方案

使用UnityWebRequest

可以用MFramework api

MFramework.DownloadAsset.DownLoadAssetsByURL<T>(string url, Action<T> downSucCallback, Action downFailCallback = null) where T : class

具体实现

    private IEnumerator DownloadByURL<T>(string url, Action<T> downSucCallback, Action downFailCallback = null) where T : class
         {
             using UnityWebRequest request = UnityWebRequest.Get(url);
             var type = typeof(T);
             #region 判定T 资源类型
             //图片文件
             if (type == typeof(Texture2D) || type == typeof(Sprite))
             {
                 request.downloadHandler = new DownloadHandlerTexture(true);
             }
             //音频文件
             else if (type == typeof(DownloadHandlerAudioClip) || type == typeof(AudioClip))
             {
                 //Debug.Log("解析URL音频资源 默认音频类型为WAV,如需更改需在此设置");
                 //request.downloadHandler = new DownloadHandlerAudioClip(url, AudioType.MPEG);
                 request.downloadHandler = new DownloadHandlerAudioClip(url, AudioType.WAV);
             }
             //本地文本文件
             else if (type == typeof(string))
             {
 ​
             }
 ​
             #endregion
             //读取资源
             yield return request.SendWebRequest();
             if (request.result == UnityWebRequest.Result.ProtocolError
                 || request.result == UnityWebRequest.Result.ConnectionError)
             {
                 Debug.Log(request.error + "检查 URL " + url);
                 downFailCallback?.Invoke();
             }
             else
             {
                 if (request.isDone)
                 {  //资源实体 回调参数 
                     T changeType = null;
                     #region 获取资源实体
                     //判定T 资源类型
                     if (type == typeof(Texture2D) || type == typeof(Sprite))//图片资源
                     {
                         //获取资源
                         DownloadHandlerTexture downloadHandlerTexture = (DownloadHandlerTexture)request.downloadHandler;
                         if (type == typeof(Texture2D))
                         {
                             //强转为资源类型
                             changeType = downloadHandlerTexture.texture as T;
                         }
                         else if (type == typeof(Sprite))
                         {
                             Sprite sprite = Sprite.Create(downloadHandlerTexture.texture, new Rect(0, 0, downloadHandlerTexture.texture.width, downloadHandlerTexture.texture.height), new Vector2(0.5f, 0.5f));
                             changeType = sprite as T;
                         }
 ​
                     }
                     else if (type == typeof(DownloadHandlerAudioClip) || type == typeof(AudioClip))//音频资源
                     {
                         DownloadHandlerAudioClip audioClip = (DownloadHandlerAudioClip)request.downloadHandler;
                         if (type == typeof(DownloadHandlerAudioClip))
                         {
                             changeType = audioClip as T;
                         }
                         else if (type == typeof(AudioClip))
                         {
                             changeType = audioClip.audioClip as T;
                         }
                     }
                     else if (type == typeof(string))//文本资源
                     {
                         string txt = request.downloadHandler.text;
                         changeType = txt as T;
                     }
                     #endregion
                     //卸载未使用的资源
                     Resources.UnloadUnusedAssets();
                     //加载成功 回调 传出资源
                     downSucCallback?.Invoke(changeType);
                 }
             }
         }
     }

评论