예제 #1
0
        /// <summary>
        /// 获取到某个资源
        /// </summary>
        /// <param name="path">资源路径</param>
        /// <param name="onSucc">成功时的回调</param>
        /// <param name="onFail">失败时的回调</param>
        /// <param name="resType">资源类型,默认是UnKnow,如果是在bundle模式下,并且传入Unknow类型,会改成AssetBundle类型</param>
        /// <returns>返回当前加载的Resource</returns>
        public Resource GetResource(string path, ResourceHandler onSucc = null, ResourceHandler onFail = null,
                                    ResourceType resType = ResourceType.UnKnow)
        {
            if (string.IsNullOrEmpty(path))
            {
                CLog.LogError("[GetResource]ResName can not is null!");
                return(null);
            }

            Resource res;

            _mapRes.TryGetValue(GetCacheResourceKey(path), out res);
            if (res != null)
            {
                if (res.isDone)
                {
                    if (res.isSucc && onSucc != null)
                    {
                        ResourceHandler tempOnSucc = onSucc;
                        onSucc = null;
                        tempOnSucc.Invoke(res);
                        tempOnSucc = null;
                    }
                }
                else
                {
                    AddListener(res, onSucc, onFail);
                }
                return(res);
            }
            res         = new Resource();
            res.path    = path;
            res.resType = (!ResourcesLoadMode && resType == ResourceType.UnKnow) ? ResourceType.AssetBundle : resType;

            //获取到当前资源的依赖资源(可能没法保证顺序,所以拿的时候需要保证所有依赖资源都已经加载好)
            if (!ResourcesLoadMode && res.resType == ResourceType.AssetBundle)
            {
                string[] listDependResPath = GetDependResPath(res);
                if (listDependResPath != null && listDependResPath.Length > 0)
                {
                    List <Resource> listDependRes = new List <Resource>();
                    for (int i = 0; i < listDependResPath.Length; i++)
                    {
                        //加载依赖资源
                        Resource dependRes = GetResource(listDependResPath[i]);
                        listDependRes.Add(dependRes);
                    }
                    res.SetDependsRes(listDependRes);
                }
            }
            //真正加载当前资源
            _mapRes.Add(GetCacheResourceKey(res.path), res);
            res.Retain();
            AddListener(res, onSucc, onFail);
            _resLoader.Load(res);
            return(res);
        }
        private void OnFinish(Resource res)
        {
            if (res.isSucc)
            {
                res.Retain();
                _mapRes.Add(res.path, res);
            }
            else
            {
                CLog.LogError("[MultiResourceLoader] load " + res.path + " fail!");
            }
            _finishCount++;
            List <Action <Resource> > list;

            _mapTryGetRes.TryGetValue(res.path, out list);
            if (list != null)
            {
                _mapTryGetRes.Remove(res.path);
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Invoke(res);
                }
            }
            if (_OnProgress != null)
            {
                Action <Resource> tempAction = _OnProgress;
                tempAction.Invoke(res);
            }
            if (_finishCount == _loadList.Count)
            {
                foreach (var item in _mapTryGetRes)
                {
                    Resource tempRes;
                    _mapRes.TryGetValue(item.Key, out tempRes);
                    if (tempRes == null)
                    {
                        continue;
                    }
                    for (int i = 0; i < item.Value.Count; i++)
                    {
                        item.Value[i].Invoke(tempRes);
                    }
                }
                _mapTryGetRes.Clear();
                if (_OnComplete != null)
                {
                    Action <MultiResourceLoader> tempAction = _OnComplete;
                    _OnComplete = null;
                    tempAction.Invoke(this);
                }
            }
        }
예제 #3
0
 static int Retain(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         Launch.Resource obj = (Launch.Resource)ToLua.CheckObject(L, 1, typeof(Launch.Resource));
         obj.Retain();
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }