예제 #1
0
        public static void RemoveReference(UnityEngine.Object reference, FMDownloadObject obj = null)
        {
            var instanceId = reference.GetInstanceID().ToString();

            if (DownloadReferences.ContainsKey(instanceId))
            {
                if (obj == null)
                {
                    foreach (var id in DownloadReferences[instanceId])
                    {
                        if (CurrentDownloads.ContainsKey(id))
                        {
                            CurrentDownloads[id].NullCallBacks();
                        }
                    }
                }
                else
                {
                    if (CurrentDownloads.ContainsKey(obj.Id))
                    {
                        CurrentDownloads[obj.Id].NullCallBacks();
                    }
                }

                DownloadReferences.Remove(instanceId);
            }
        }
예제 #2
0
        static void OnComplete(FMDownloadObject obj)
        {
            var id = obj.Id;

            RemoveReference(obj.Owner, obj);
            if (CurrentDownloads.ContainsKey(id))
            {
                CurrentDownloads[id] = null;
                CurrentDownloads.Remove(id);
            }
        }
예제 #3
0
        bool GetFile(UnityEngine.Object owner, object onComplete, Action onFail = null, Action <float> onProgress = null, bool needDataOnDownload = true, params string[] urls)
        {
            if (urls.Length <= 0)
            {
                onFail.SafeInvoke();
                return(false);
            }

            string id      = string.Empty;
            var    urlList = urls.ToList().Where(x => x != string.Empty).ToList();

            if (urlList.Count == 1)
            {
                id = new Uri(urlList[0]).AbsoluteUri.GetUniqueId();
                if (GetFileType(urlList[0]) == EFileType.Invalid)
                {
                    onFail.SafeInvoke();
                    return(false);
                }
            }
            else
            {
                id = urlList.GetUniqueId(); //urlList.Aggregate((i, j) => new Uri(i).AbsoluteUri + new Uri(j).AbsoluteUri).GenerateUniqueId();

                foreach (var url in urlList)
                {
                    if (GetFileType(url) == EFileType.Invalid)
                    {
                        onFail.SafeInvoke();
                        return(false);
                    }
                }
            }

            List <string> toDownload = new List <string>();

            foreach (var url in urlList)
            {
                if (!IsFileDownloaded(url))
                {
                    toDownload.Add(url);
                }
            }

            if (toDownload.Any())
            {
                if (Application.internetReachability == NetworkReachability.NotReachable)
                {
                    onFail.SafeInvoke();
                    return(false);
                }

                var instanceId = owner.GetInstanceID().ToString();
                if (DownloadReferences.ContainsKey(instanceId))
                {
                    DownloadReferences[instanceId].Add(id);
                }
                else
                {
                    DownloadReferences.Add(instanceId, new List <string> {
                        id
                    });
                }

                if (!CurrentDownloads.ContainsKey(id))
                {
                    CurrentDownloads.Add(id, FMDownloadObject.CreateRequest(urlList.ToList(), onComplete, owner, id, onProgress, onFail, needDataOnDownload).StartDownload());
                    CurrentDownloads[id].OnDownloadComplete += OnComplete;
                }
                else
                {
                    CurrentDownloads[id].AppendDownload(urlList[0], onComplete, onProgress, onFail);
                }
            }

            return(!toDownload.Any());
        }