Пример #1
0
        GameObject GetGameObject(string resName, ResourceType type)
        {
            if (_instancePool.ContainsKey(resName) == false)
            {
                Queue <GameObject> queue = new Queue <GameObject>();
                _instancePool.Add(resName, queue);
            }
            Queue <GameObject> pool   = _instancePool[resName];
            GameObject         result = null;

            if (pool.Count > 0)
            {
                result = pool.Dequeue();
                result.SetActive(true);
            }
            else
            {
                System.Type resType = typeof(GameObject);
                result = GameObject.Instantiate(_objPool[resType][resName]) as GameObject;
                ResInfo info = new ResInfo();
                info.ResName = resName;
                info.type    = type;
                _resNameOfObject[result.GetInstanceID()] = info;
            }

            return(result);
        }
Пример #2
0
        void DealResAfterLoaded(string resName, ResourceType type, System.Type resType, Object obj)
        {
            if (resType == typeof(AssetBundle) && _assetbundleList.Contains(resName) == false)
            {
                _assetbundleList.Add(resName);
            }
            ResInfo info = new ResInfo();

            info.ResName = resName;
            info.type    = type;
            _resNameOfObject[obj.GetInstanceID()] = info;
        }
Пример #3
0
        public Sprite SyncGetSpecifyImgInAltas(string resName, int type)
        {
            if (string.IsNullOrEmpty(resName))
            {
                return(null);
            }

            StringBuilder path    = GetAltasPath(resName, type);
            string        pathStr = path.ToString();

            if (_altasPool.ContainsKey(pathStr) == false)
            {
                SyncLoadSpecifyAltas(path);
            }

            for (int i = 0, count = _altasPool[pathStr].Length; i < count; ++i)
            {
                if (_altasPool[pathStr][i].name.CompareTo(resName) == 0 && _altasPool[pathStr][i] is Sprite)
                {
                    int instanceId = _altasPool[pathStr][i].GetInstanceID();
                    if (_resNameOfObject.ContainsKey(instanceId) == false)
                    {
                        ResInfo res = new ResInfo();
                        res.ResName = pathStr;
                        res.type    = ResourceType.Sprite;
                        _resNameOfObject[instanceId] = res;
                    }
                    if (_altasReference.ContainsKey(pathStr) == false)
                    {
                        _altasReference[pathStr] = 1;
                    }
                    else
                    {
                        ++_altasReference[pathStr];
                    }
                    return(_altasPool[pathStr][i] as Sprite);
                }
            }

            return(null);
        }
Пример #4
0
 /// <summary>
 /// 简单的判断资源是否需要实例化
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="resName"></param>
 /// <param name="type"></param>
 /// <returns></returns>
 Object GetInstance(string resName, ResourceType type, System.Type resType)
 {
     if (CheckPool(resName, resType) == false)
     {
         return(default(Object));
     }
     else if (NeedObjectPool(type))
     {
         return(GetGameObject(resName, type));
     }
     else if (resType == typeof(GameObject))
     {
         GameObject go   = GameObject.Instantiate(_objPool[resType][resName]) as GameObject;
         ResInfo    info = new ResInfo();
         info.ResName = resName;
         info.type    = type;
         if (_gameObjectRefCount.ContainsKey(resName) == false)
         {
             _gameObjectRefCount[resName] = 0;
         }
         ++_gameObjectRefCount[resName];
         _resNameOfObject[go.GetInstanceID()] = info;
         return(go);
     }
     else
     {
         Object value = _objPool[resType][resName];
         DealResAfterLoaded(resName, type, resType, value);
         //ResInfo info = new ResInfo();
         //info.ResName = resName;
         //info.type = type;
         //_resNameOfObject[value.GetInstanceID()] = info;
         //if (resType == typeof(AssetBundle) && _assetbundleList.Contains(resName) == false)
         //    _assetbundleList.Add(resName);
         return(value);
     }
 }
Пример #5
0
        bool UnloadResource(ResInfo info, Object obj, bool forceUnload)
        {
            System.Type type;
            if (_assetbundleList.Contains(info.ResName))
            {
                type = typeof(AssetBundle);
                _assetbundleList.Remove(info.ResName);
            }
            else
            {
                type = obj.GetType();
            }

            UnloadDependencyAssetBundle(info.ResName);

            ///如果是卸载的图集,需要判断所有引用都卸载了,再卸载图集
            if (_altasReference.ContainsKey(info.ResName))
            {
                --_altasReference[info.ResName];

                if (_altasReference[info.ResName] == 0)
                {
                    if (DevelopSetting.IsLoadAB)
                    {
                        AssetBundle res = _objPool[type][info.ResName] as AssetBundle;
                        res.Unload(true);
                        _objPool[type].Remove(info.ResName);
                    }

                    if (_altasPool.ContainsKey(info.ResName))
                    {
                        _altasPool.Remove(info.ResName);
                    }
                    _altasReference.Remove(info.ResName);

                    Resources.UnloadUnusedAssets();
                }
                return(true);
            }
            else if (CheckPool(info.ResName, type))
            {
                if (NeedObjectPool(info.type) && forceUnload == false)
                {
                    return(RecycleObject(info.ResName, obj as GameObject));
                }
                else if (type == typeof(GameObject))
                {
                    GameObject.Destroy(obj);
                    if (_gameObjectRefCount.ContainsKey(info.ResName))
                    {
                        --_gameObjectRefCount[info.ResName];
                        if (_gameObjectRefCount[info.ResName] == 0)
                        {
                            _objPool[type].Remove(info.ResName);
                            _gameObjectRefCount.Remove(info.ResName);
                        }
                    }
                }
                else
                {
                    if (type == typeof(AssetBundle))
                    {
                        AssetBundle res = _objPool[type][info.ResName] as AssetBundle;
                        res.Unload(true);
                    }

                    else if (type == typeof(Sprite))
                    {
                        Object res    = _objPool[type][info.ResName];
                        Sprite sprite = res as Sprite;
                        Resources.UnloadAsset(sprite.texture);
                        GameObject.Destroy(res);
                    }
                    else
                    {
                        Object res = _objPool[type][info.ResName];
                        Resources.UnloadAsset(res);
                        GameObject.Destroy(obj as GameObject);
                    }

                    _objPool[type].Remove(info.ResName);
                }
                Resources.UnloadUnusedAssets();
                return(true);
            }

            Resources.UnloadUnusedAssets();
            return(false);
        }