UnityWebRequest 下载阿里云资源写入本地 与 加载资源

using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using System.Collections.Generic;

namespace DownloadResources
{
    internal class EduResDownloader
    {
        private static EduResDownloader _Instance = null;
        private static readonly object locker = new object();
        private readonly string serverUrl = "https://............";
        private Dictionary<string, AudioClip> cacheAudioClips = new Dictionary<string, AudioClip>();
        public static EduResDownloader Instance
        {
            get
            {
                if (_Instance == null)
                {
                    lock (locker)
                    {
                        if (_Instance == null)
                        {
                            _Instance = new EduResDownloader();
                        }
                    }
                }
                return _Instance;
            }
        }
        #region private methods
        private EduResDownloader() { }

        private void CheckAndCreateDirectory(string path)
        {
            string localPath = GetSaveLocalPathPre();
            string str = path.Substring(localPath.LastIndexOf('/') + 1);
            string[] strs = str.Split('/');
            string itemPath = localPath;
            for (int i = 0; i < strs.Length - 1; i++)
            {
                itemPath = Path.Combine(itemPath, strs[i]);
                if (!Directory.Exists(itemPath))
                {
                    Directory.CreateDirectory(itemPath);
                }
            }
        }

        private IEnumerator DownloadFromLocal<T>(string url, System.Action<T> callBack = null) where T : UnityEngine.Object
        {
            url = url.Replace("\\", "/");
            string localPath = GetSaveLocalPathPre() + url;
            if (File.Exists(localPath))
            {
                if (typeof(T) == typeof(Sprite))
                {
                    yield return DownloadLocalSprite(url, (s) =>
                    {
                        if (callBack != null)
                        {
                            callBack(s as T);
                        }
                    });
                }
                else if (typeof(T) == typeof(Texture2D))
                {
                    yield return DownloadLocalTexture2D(url, (s) =>
                    {
                        if (callBack != null)
                        {
                            callBack(s as T);
                        }
                    });
                }
                else if (typeof(T) == typeof(AudioClip))
                {
                    yield return DownloadLocalAudioClip(url, (s) =>
                    {
                        if (callBack != null)
                        {
                            callBack(s as T);
                        }
                    });
                }
                else
                {
                    Debug.LogError("Now do not support T = " + typeof(T).ToString());
                }
            }
        }
        private IEnumerator DownloadByUrl<T>(string url, System.Action<T> callBack = null) where T : UnityEngine.Object
        {
            url = url.Replace("\\", "/");
            string localPath = GetSaveLocalPathPre() + url;
            if (!File.Exists(localPath))
            {
                yield return DownloadFormServer(url);
            }
            yield return DownloadFromLocal(url, callBack);

        }

        private IEnumerator DownloadLocalSprite(string url, System.Action<Sprite> callBack = null)
        {
            string readLocalPath = GetReadLocalPathPre() + url;
            UnityWebRequest webRequest = UnityWebRequest.Get(readLocalPath);
            DownloadHandlerTexture handlerTexture = new DownloadHandlerTexture(true);
            webRequest.downloadHandler = handlerTexture;
            webRequest.timeout = 20;
            yield return webRequest.SendWebRequest();
            if (webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error))
            {
                Debug.LogError("DownLocalSprite Error : " + webRequest.error + "  url = " + readLocalPath);
            }
            else
            {
                Texture2D texture2D = null;
                texture2D = handlerTexture.texture;
                Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.one * 0.5f);
                if (callBack != null)
                {
                    callBack(sprite);
                }
            }
            webRequest.Dispose();
        }

        private IEnumerator DownloadLocalTexture2D(string url, System.Action<Texture2D> callBack = null)
        {
            string readLocalPath = GetReadLocalPathPre() + url;
            UnityWebRequest webRequest = UnityWebRequest.Get(readLocalPath);
            DownloadHandlerTexture handlerTexture = new DownloadHandlerTexture(true);
            webRequest.downloadHandler = handlerTexture;
            webRequest.timeout = 20;
            yield return webRequest.SendWebRequest();
            if (webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error))
            {
                Debug.LogError("DownloadLocalTexture2D Error : " + webRequest.error + "  url = " + readLocalPath);
            }
            else
            {
                Texture2D texture2D = null;
                texture2D = handlerTexture.texture;
                if (callBack != null)
                {
                    callBack(texture2D);
                }
            }
            webRequest.Dispose();
        }

        private IEnumerator DownloadLocalAudioClip(string url, System.Action<AudioClip> callBack = null)
        {
            string readLocalPath = GetReadLocalPathPre() + url;
            UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(readLocalPath, AudioType.MPEG);
            webRequest.timeout = 20;
            yield return webRequest.SendWebRequest();
            if (webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error))
            {
                Debug.LogError("DownloadLocalAudioClip Error : " + webRequest.error + "  url = " + readLocalPath);
            }
            else
            {
                AudioClip audioClip = DownloadHandlerAudioClip.GetContent(webRequest);
                if (callBack != null)
                {
                    callBack(audioClip);
                }
            }
            webRequest.Dispose();
        }

        private string GetSaveLocalPathPre()
        {
            return Application.persistentDataPath + "/";
        }

        private string GetReadLocalPathPre()
        {
            string path =
#if UNITY_EDITOR
            "file:///" + Application.persistentDataPath + "/";
#elif UNITY_ANDROID
            "file://" + Application.persistentDataPath + "/";
#else
            "file:///" + Application.persistentDataPath + "/";
#endif
            return path;
        }

        #endregion

        #region public methods
        /// <summary>
        /// 下载阿里云资源或从本地沙盒
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="url">路径加文件名</param>
        /// <param name="callBack">下载完回调</param>
        public IEnumerator LoadResByUrl<T>(string url, System.Action<T> callBack = null) where T : UnityEngine.Object
        {
            yield return DownloadByUrl(url, callBack);
        }

        public IEnumerator DownloadFormServer(string url, System.Action<bool> callBack = null)
        {
            url = url.Replace("\\", "/");
            string localPath = GetSaveLocalPathPre() + url;
            if (File.Exists(localPath))
            {
                if (callBack != null)
                {
                    callBack(true);
                }
                yield break;
            }

            string serverPath = serverUrl + url;
            UnityWebRequest webRequest = UnityWebRequest.Get(serverPath);
            webRequest.timeout = 20;
            yield return webRequest.SendWebRequest();

            if (webRequest.isNetworkError || !string.IsNullOrEmpty(webRequest.error))
            {
                Debug.LogError("DownloadFormServer NetworkError : " + webRequest.error + "   url = " + serverPath);
                if (callBack != null)
                {
                    callBack(false);
                }
            }
            else
            {
                var file = webRequest.downloadHandler.data;

                CheckAndCreateDirectory(localPath);

                //try
                //{
                //    FileStream fileStream = new FileStream(localPath, FileMode.Create);
                //    fileStream.Write(file, 0, file.Length);
                //    fileStream.Flush();
                //    fileStream.Close();
                //}
                //catch (System.Exception e)
                //{
                //    Debug.Log(e.ToString());
                //}

                File.WriteAllBytes(localPath, file);

                if (callBack != null)
                {
                    callBack(true);
                }
            }
            webRequest.Dispose();
        }

        public IEnumerator CacheAudioClip(string url)
        {
            url = url.Replace("\\", "/");
            yield return DownloadLocalAudioClip(url, (clip) =>
             {
                 cacheAudioClips[url] = clip;
             });
        }

        public AudioClip LoadAudioClip(string url)
        {
            url = url.Replace("\\", "/");
            AudioClip audioClip;
            if (cacheAudioClips.TryGetValue(url, out audioClip))
            {
                return audioClip;
            }
            return null;
        }

        public void Destroy()
        {
            cacheAudioClips.Clear();
            _Instance = null;
        }
        #endregion
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值