Esempio n. 1
0
        public GameObject InstantiateObject(string path, bool isShowDefaultTrans = false, bool isClear = true)
        {
            uint       crc        = CRC32.GetCRC32(path);
            ObjectItem objectItem = GetCacheObjectItemFromPoolDic(crc);

            if (ReferenceEquals(objectItem, null))
            {
                objectItem         = objectItemNativePool.Spawn();
                objectItem.Crc     = crc;
                objectItem.isClear = isClear;
                objectItem         = ResourceManager.Instance.LoadPrimitiveAssetItem(path, objectItem);

                var PrimitiveAssetObject = objectItem.PrimitiveAssetItem.AssetObject;
                if (PrimitiveAssetObject != null)
                {
                    objectItem.CloneObj    = GameObject.Instantiate(PrimitiveAssetObject) as GameObject;
                    objectItem.OfflineData = objectItem.CloneObj.GetComponent <OfflineData.OfflineData>();
                }
            }

            if (isShowDefaultTrans)
            {
                objectItem.CloneObj.transform.SetParent(SceneTrans, false);
            }

            int guid = objectItem.CloneObj.GetInstanceID();

            if (ObjectItemsInstanceTempDic.ContainsKey(guid) == false)
            {
                ObjectItemsInstanceTempDic.Add(guid, objectItem);
            }

            return(objectItem.CloneObj);
        }
Esempio n. 2
0
        private AssetBundle LoadAssetBundle(string name)
        {
            AssetBundleItem item = null;
            uint            crc  = CRC32.GetCRC32(name);

            if (!AssetBundleItemDic.TryGetValue(crc, out item))
            {
                AssetBundle assetBundle = null;
                string      path        = P.GetFullPath(Application.streamingAssetsPath, name);
                if (File.Exists(path))
                {
                    assetBundle = AssetBundle.LoadFromFile(path);
                }

                if (assetBundle == null)
                {
                    Debug.LogError($"{MethodBase.GetCurrentMethod().Name} Error: {path} cant load");
                }

                item             = AssetBundleItemPool.Spawn();
                item.AssetBundle = assetBundle;
                AssetBundleItemDic.Add(crc, item);
            }

            item.RefCount++;
            return(item.AssetBundle);
        }
Esempio n. 3
0
        //给ObjectManager的接口
        public ObjectItem LoadPrimitiveAssetItem(string path, ObjectItem objectItem)
        {
            if (objectItem == null)
            {
                return(null);
            }
            uint crc = objectItem.Crc == 0 ? CRC32.GetCRC32(path) : objectItem.Crc;

            AssetItem assetItem = GetCacheAssetItem(crc);

            if (assetItem != null)
            {
                objectItem.PrimitiveAssetItem = assetItem;
                return(objectItem);
            }

            Object obj = null;

#if UNITY_EDITOR
            if (!IsLoadFromAssetBundle)
            {
                assetItem = AssetBundleManager.Instance.FindAssetItem(crc);
                if (assetItem != null && assetItem.AssetObject != null)
                {
                    obj = assetItem.AssetObject;
                }
                else
                {
//                if (assetItem == null)
//                {
//                    assetItem=new AssetItem();
//                }
                    obj = LoadAssetByEditor <Object>(path);
                }
            }
#endif
            if (obj == null)
            {
                assetItem = AssetBundleManager.Instance.LoadAssetItemBundle(crc);
                if (assetItem != null && assetItem.assetBundle != null)
                {
                    if (assetItem.AssetObject != null)
                    {
                        obj = assetItem.AssetObject as Object;
                    }
                    else
                    {
                        obj = assetItem.assetBundle.LoadAsset <Object>(assetItem.assetName);
                    }
                }
            }

            assetItem.IsClear = objectItem.isClear;
            CacheResource(path, ref assetItem, crc, obj);
            objectItem.PrimitiveAssetItem = assetItem;

            return(objectItem);
        }
Esempio n. 4
0
        public void PreLoadRes(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            uint      crc  = CRC32.GetCRC32(path);
            AssetItem item = GetCacheAssetItem(crc, 0);

            if (item != null)
            {
//            item.IsClear = false;
                return;
            }

            Object obj = null;

#if UNITY_EDITOR
            if (!IsLoadFromAssetBundle)
            {
                item = AssetBundleManager.Instance.FindAssetItem(crc);


                if (item.AssetObject != null)
                {
                    obj = item.AssetObject;
                }
                else
                {
                    obj = LoadAssetByEditor <Object>(path);
                }
            }
#endif
            if (obj == null)
            {
                item = AssetBundleManager.Instance.LoadAssetItemBundle(crc);
                if (item != null && item.assetBundle != null)
                {
                    if (item.AssetObject != null)
                    {
                        obj = item.AssetObject;
                    }
                    else
                    {
                        obj = item.assetBundle.LoadAsset <Object>(item.assetName);
                    }
                }
            }

            CacheResource(path, ref item, crc, obj);
            //跳场景
            item.IsClear = false;
            ReleaseResource(path, false);
        }
Esempio n. 5
0
        public T LoadResource <T>(string path) where T : Object
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }

            uint      crc  = CRC32.GetCRC32(path);
            AssetItem item = GetCacheAssetItem(crc);

            if (item != null)
            {
                return(item.AssetObject as T);
            }

            T obj = null;

#if UNITY_EDITOR
            if (!IsLoadFromAssetBundle)
            {
                item = AssetBundleManager.Instance.FindAssetItem(crc);
                if (item != null && item.AssetObject != null)
                {
                    obj = item.AssetObject as T;
                }
                else
                {
                    obj = LoadAssetByEditor <T>(path);
                }
            }
#endif
            if (obj == null)
            {
                item = AssetBundleManager.Instance.LoadAssetItemBundle(crc);
                if (item != null && item.assetBundle != null)
                {
                    if (item.AssetObject != null)
                    {
                        obj = item.AssetObject as T;
                    }
                    else
                    {
                        obj = item.assetBundle.LoadAsset <T>(item.assetName);
                    }
                }
            }

            CacheResource(path, ref item, crc, obj);
            return(obj);
        }
Esempio n. 6
0
        private void UnLoadAssetBundle(string name)
        {
            uint            crc = CRC32.GetCRC32(name);
            AssetBundleItem item;

            if (AssetBundleItemDic.TryGetValue(crc, out item) && item != null)
            {
                item.RefCount--;
                if (item.RefCount <= 0 && item.AssetBundle != null)
                {
                    item.AssetBundle.Unload(true);
                    item.Rest();
                    AssetBundleItemPool.Recycle(item);
                    AssetBundleItemDic.Remove(crc);
                }
            }
        }
Esempio n. 7
0
        public bool ReleaseResource(string path, bool isDestroyCache = false)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(false);
            }
            uint      crc  = CRC32.GetCRC32(path);
            AssetItem item = null;

            if (!AssetDic.TryGetValue(crc, out item))
            {
                Debug.LogError("AssetDic not exits " + path + ",可能进行了多次释放");
                return(false);
            }

            item.RefCount--;
            DestroyAsset(item, isDestroyCache);
            return(true);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }