Ejemplo n.º 1
0
        /// <summary>
        /// 针对ObjectManager异步加载的接口
        /// </summary>
        /// <param name="path"></param>
        /// <param name="resObj"></param>
        /// <param name="dealfinish"></param>
        /// <param name="priority"></param>
        public void AsyncLoadResouce(string path, TResouceObj resObj, OnAsyncFinish dealfinish, LoadResPriority priority)
        {
            TResouceItem item = GetCacheResouceItem(resObj.Crc);

            if (item != null)
            {
                resObj.ResItem = item;
                dealfinish?.Invoke(path, resObj);
            }
            //判断是否在加载中
            TAsyncLoadResParam para = null;

            if (!mLoadingAssetDic.TryGetValue(resObj.Crc, out para) || para == null)
            {
                para          = mAsyncLoadResParamPool.Allocate();
                para.Crc      = resObj.Crc;
                para.Path     = path;
                para.Priority = priority;
                mLoadingAssetDic.Add(resObj.Crc, para);
                mLoadingAssetList[(int)priority].Add(para);
            }
            //往回调列表里面加回调
            TAsyncCallBack callBack = mAsyncCallBackPool.Allocate();

            callBack.DealFinish = dealfinish;
            callBack.ResObj     = resObj;
            para.CallbackList.Add(callBack);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 异步资源加载,外部直接调用,(仅仅加载不需要实例化的资源,例如Texture和音频之类的)
        /// </summary>
        public void AsyncLoadResource(string path, OnAsyncFinish dealFinish, LoadResPriority priority, bool isSprite = false, uint crc = 0, params object[] paramList)
        {
            if (crc == 0)
            {
                crc = Crc32.GetCrc32(path);
            }

            AssetBundleInfo abInfo = GetCacheAssetBundleInfo(crc);

            if (abInfo != null)
            {
                dealFinish?.Invoke(path, abInfo.m_Obj, paramList);
                return;
            }

            //判断是否在加载中
            AsyncLoadResUnit unit = null;

            //没有找到这个异步加载单位,或者这个异步加载单位是空
            if (!m_LoadingAssetDic.TryGetValue(crc, out unit) || unit == null)
            {
                unit            = m_AsyncLoadResParamPool.Spawn(true);
                unit.m_Crc      = crc;
                unit.m_Path     = path;
                unit.m_Sprite   = isSprite;
                unit.m_Priority = priority;

                m_LoadingAssetDic.Add(crc, unit);            //添加到正在异步加载的资源dic中

                m_loadingAssetList[(int)priority].Add(unit); //按照加载优先级,添加到对应的正在异步加载的资源列表中
            }

            //往回调列表里面添加回调
            AsynCallBack callBack = m_AsynCallBackPool.Spawn(true);

            callBack.m_DealFinish = dealFinish;
            callBack.m_Params     = paramList;

            //往这个异步加载单位的回调列表中添加一个回调
            //可能多个地方加载同一份资源,这样做只加载一次资源,
            //加载完了后,根据回调列表一次返回这份资源
            unit.m_CallBackList.Add(callBack);
        }
Ejemplo n.º 3
0
        public void AsyncLoadResource(string path, OnAsyncFinish cb, LoadResPriority priority, bool isSprite = false,
                                      uint crc = 0,
                                      params object[] paramList)
        {
            if (crc == 0)
            {
                crc = CRC32.GetCRC32(path);
            }

            AssetItem item = GetCacheAssetItem(crc);

            if (item != null)
            {
                cb?.Invoke(path, item.AssetObject, paramList);
                return;
            }

            AsyncLoadAssetParam asyncLoadAssetParam = null;

            if (!asyncLoadAssetParamDic.TryGetValue(crc, out asyncLoadAssetParam))
            {
                asyncLoadAssetParam          = asyncLoadResParamPackPool.Spawn();
                asyncLoadAssetParam.Crc      = crc;
                asyncLoadAssetParam.Path     = path;
                asyncLoadAssetParam.isSprite = isSprite;
                asyncLoadAssetParam.Priority = priority;
                asyncLoadAssetParamDic.Add(crc, asyncLoadAssetParam);
                loadingAssetLists[(int)priority].Add(asyncLoadAssetParam);
            }

            AsyncCallBackPack callBackPack = asyncCallBackPackPool.Spawn();

            callBackPack.AssetFinish = cb;
            callBackPack.paramList   = paramList;
            asyncLoadAssetParam.CallBackPacks.Add(callBackPack);
        }
Ejemplo n.º 4
0
        public long AsyncInstantiateObject(string path, OnAsyncFinish outerCallBack, LoadResPriority priority,
                                           bool isSetSceneTrans = false, bool isClear = true, params object[] paramList)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }
            uint       crc        = CRC32.GetCRC32(path);
            ObjectItem objectItem = GetCacheObjectItemFromPoolDic(crc);

            if (objectItem != null)
            {
                if (isSetSceneTrans)
                {
                    objectItem.CloneObj.transform.SetParent(SceneTrans, false);
                }
                outerCallBack?.Invoke(path, objectItem.CloneObj, paramList);
                return(objectItem.Guid);
            }

            objectItem = objectItemNativePool.Spawn();
            if (objectItem == null)
            {
                Debug.LogError("null");
            }

            long guid = ResourceManager.Instance.CreateGuid();

            objectItem.Crc = crc;
            objectItem.IsSetSceneParent = isSetSceneTrans;
            objectItem.isClear          = isClear;
            objectItem.outerCallBack    = outerCallBack;
            objectItem.paramList        = paramList;
            //调用ResourceManager异步加载接口

            ResourceManager.Instance.AsyncLoadResource(path, objectItem, (_path, item, plist) =>
            {
                if (item == null)
                {
                    return;
                }
                if (item.PrimitiveAssetItem.AssetObject == null)
                {
#if UNITY_EDITOR
                    Debug.LogError("异步资源加载为空" + _path);
#endif
                }
                else
                {
                    //实例化
                    item.CloneObj          = Object.Instantiate(item.PrimitiveAssetItem.AssetObject) as GameObject;
                    objectItem.OfflineData = item.CloneObj.GetComponent <OfflineData.OfflineData>();
                }

//加载完成移除
                if (asyncLoadingObjectDic.ContainsKey(item.Guid))
                {
                    asyncLoadingObjectDic.Remove(item.Guid);
                }
                if (item.CloneObj != null && item.IsSetSceneParent)
                {
                    item.CloneObj.transform.SetParent(SceneTrans);
                }

                if (item.outerCallBack != null)
                {
                    if (item.CloneObj != null)
                    {
                        int _guid = item.CloneObj.GetInstanceID();
                        if (!ObjectItemsInstanceTempDic.ContainsKey(_guid))
                        {
                            ObjectItemsInstanceTempDic.Add(_guid, item);
                        }
                    }

                    item.outerCallBack?.Invoke(_path, item.CloneObj, plist);
                }
            }, priority);
            return(guid);
        }