示例#1
0
        public static void ClearByAssetPrefix(string prefix)
        {
            var clearAssetList = new List <string>();

            var removedCacheList = _assetCacheDict.Where(_ => _.Key.Contains(prefix)).ToArray();

            foreach (var cache in removedCacheList)
            {
                cache.Value.Clear();
                _assetCacheDict.Remove(cache.Key);

                if (clearAssetList.IndexOf(cache.Key) == -1)
                {
                    clearAssetList.Add(cache.Key);
                }
            }

            var removedAssetList = _loadedAssetDict.Where(_ => _.Key.Contains(prefix)).ToArray();

            foreach (var asset in removedAssetList)
            {
                var instance = asset.Value.instance;
                if (instance != null)
                {
                    if (!instance.name.Contains(AssetConstant.UNUSED_ASSET_FLAG))
                    {
                        (instance as GameObject)?.Despawn();
                    }
                    UObject.Destroy(asset.Value.instance);
                }

                _loadedAssetDict.Remove(asset.Key);

                if (clearAssetList.IndexOf(asset.Key) == -1)
                {
                    clearAssetList.Add(asset.Key);
                }
            }

            var removedAssetNameList = _assetNameDict.Where(_ => clearAssetList.IndexOf(_.Value) != -1)
                                       .Select(_ => _.Key).ToArray();

            foreach (var asset in removedAssetNameList)
            {
                _assetNameDict.Remove(asset);
            }

            removedAssetNameList = _spawnedAssetNameDict.Where(_ => clearAssetList.IndexOf(_.Value) != -1)
                                   .Select(_ => _.Key).ToArray();
            foreach (var asset in removedAssetNameList)
            {
                GameObject.Destroy(asset);
                _spawnedAssetNameDict.Remove(asset);
            }

            foreach (var asset in clearAssetList)
            {
                _assetLoader.Clear(asset);
            }
        }