Ejemplo n.º 1
0
        /// <summary>
        /// 即将要释放的资源
        /// </summary>
        /// <param name="resource"></param>
        private void WillUnload(AResource resource)
        {
            if (m_NeedUnloadList.Count == 0)
            {
                m_NeedUnloadList.Add(resource);
                return;
            }

            bool insertFlag = false;

            //插入排序,时间大的放前面
            for (int i = 0; i < m_NeedUnloadList.Count; i++)
            {
                AResource temp = m_NeedUnloadList[i];
                if (temp.destroyTime < resource.destroyTime)
                {
                    m_NeedUnloadList.Insert(i, resource);
                    return;
                }
            }

            if (!insertFlag)
            {
                m_NeedUnloadList.Add(resource);
            }
        }
Ejemplo n.º 2
0
        public void LateUpdate()
        {
            if (m_NeedUnloadList.Count == 0)
            {
                return;
            }

            int       lastIndex = m_NeedUnloadList.Count - 1;
            AResource resource  = m_NeedUnloadList[lastIndex];

            if (now < resource.destroyTime)
            {
                return;
            }

            m_ResourceDic.Remove(resource.url);
            m_NeedUnloadList.RemoveAt(lastIndex);

            //依赖引用-1
            if (resource.dependencies != null)
            {
                for (int i = 0; i < resource.dependencies.Length; i++)
                {
                    AResource temp = resource.dependencies[i];
                    Unload(temp);
                }
            }

            resource.UnLoad();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 内部加载资源
        /// </summary>
        /// <param name="url">资源url</param>
        /// <param name="async">是否异步</param>
        /// <param name="delay">延迟释放时间</param>
        /// <param name="dependency">是否依赖</param>
        /// <returns></returns>
        private AResource LoadInternal(string url, bool async, uint delay, bool dependency)
        {
            AResource resource = null;

            if (m_ResourceDic.TryGetValue(url, out resource))
            {
                //从需要释放的列表中移除
                if (resource.reference == 0)
                {
                    m_NeedUnloadList.Remove(resource);
                }

                if (delay > resource.delay)
                {
                    resource.delay = delay;
                }
                resource.AddReference();
                return(resource);
            }

            //创建Resource
            if (m_Editor)
            {
                resource = new EditorResource();
            }
            else if (async)
            {
                ResourceAsync resourceAsync = new ResourceAsync();
                m_AsyncList.Add(resourceAsync);
                resource = resourceAsync;
            }
            else
            {
                resource = new Resource();
            }

            resource.url   = url;
            resource.delay = delay;
            m_ResourceDic.Add(url, resource);

            //加载依赖
            List <string> dependencies = null;

            ResourceDependencyDic.TryGetValue(url, out dependencies);
            if (dependencies != null && dependencies.Count > 0)
            {
                resource.dependencies = new AResource[dependencies.Count];
                for (int i = 0; i < dependencies.Count; i++)
                {
                    string    dependencyUrl      = dependencies[i];
                    AResource dependencyResource = LoadInternal(dependencyUrl, async, delay, true);
                    resource.dependencies[i] = dependencyResource;
                }
            }

            resource.AddReference();
            resource.Load();

            return(resource);
        }
Ejemplo n.º 4
0
        public void LateUpdate()
        {
            if (m_NeedUnloadList.Count == 0)
            {
                return;
            }

            while (m_NeedUnloadList.Count > 0)
            {
                AResource resource = m_NeedUnloadList.First.Value;
                m_NeedUnloadList.RemoveFirst();
                if (resource == null)
                {
                    continue;
                }

                m_ResourceDic.Remove(resource.url);

                resource.UnLoad();

                //依赖引用-1
                if (resource.dependencies != null)
                {
                    for (int i = 0; i < resource.dependencies.Length; i++)
                    {
                        AResource temp = resource.dependencies[i];
                        Unload(temp);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="url">资源Url</param>
        /// <param name="async">是否异步</param>
        /// <param name="delay">延迟释放时间</param>
        /// <param name="callback">加载完成回调</param>
        public void LoadWithCallback(string url, bool async, uint delay, Action <AResource> callback)
        {
            AResource resource = LoadInternal(url, async, delay, false);

            if (resource.done)
            {
                callback?.Invoke(resource);
            }
            else
            {
                resource.finishedCallback += callback;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="resource"></param>
        public void Unload(AResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentException($"{nameof(ResourceManager)}.{nameof(Unload)}() {nameof(resource)} is null.");
            }

            resource.ReduceReference();

            if (resource.reference == 0)
            {
                resource.destroyTime = now + resource.delay;
                WillUnload(resource);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="resource"></param>
        public void Unload(IResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentException($"{nameof(ResourceManager)}.{nameof(Unload)}() {nameof(resource)} is null.");
            }

            AResource aResource = resource as AResource;

            aResource.ReduceReference();

            if (aResource.reference == 0)
            {
                WillUnload(aResource);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 刷新异步资源(当同步资源的依赖包含异步时,需要立即刷新返回)
        /// </summary>
        internal void FreshAsyncAsset()
        {
            if (done)
            {
                return;
            }

            if (dependencies != null)
            {
                for (int i = 0; i < dependencies.Length; i++)
                {
                    AResource resource = dependencies[i];
                    resource.FreshAsyncAsset();
                }
            }

            if (this is AResourceAsync)
            {
                LoadAsset();
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="url">资源Url</param>
        /// <param name="async">是否异步</param>
        /// <param name="delay">延迟释放时间</param>
        /// <returns></returns>
        public Task <AResource> LoadTask(string url, uint delay)
        {
            AResource resource = LoadInternal(url, true, delay, false);

            if (resource.done)
            {
                if (resource.awaiter == null)
                {
                    resource.awaiter = new ResourceAwaiter(url);
                    resource.awaiter.SetResult(resource);
                }

                return(resource.awaiter.taskCompletionSource.Task);
            }

            if (resource.awaiter == null)
            {
                resource.awaiter = new ResourceAwaiter(url);
            }

            return(resource.awaiter.taskCompletionSource.Task);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="url">资源Url</param>
        /// <returns></returns>
        public ResourceAwaiter LoadWithAwaiter(string url)
        {
            AResource resource = LoadInternal(url, true, false);

            if (resource.done)
            {
                if (resource.awaiter == null)
                {
                    resource.awaiter = new ResourceAwaiter();
                    resource.awaiter.SetResult(resource as IResource);
                }

                return(resource.awaiter);
            }

            if (resource.awaiter == null)
            {
                resource.awaiter = new ResourceAwaiter();
            }

            return(resource.awaiter);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 即将要释放的资源
 /// </summary>
 /// <param name="resource"></param>
 private void WillUnload(AResource resource)
 {
     m_NeedUnloadList.AddLast(resource);
 }
Ejemplo n.º 12
0
 public void SetResult(AResource resource)
 {
     taskCompletionSource.SetResult(resource);
 }