/// <summary> /// 地图切片加载完成 /// </summary> /// <param name="loader"></param> private void OnTileComplete(MapLoader loader) { TextureAsset asset = loader.loader.asset as TextureAsset; if (asset != null) { MapLoaderItem loadItem = loader.loaderItem; if (loadItem.mapId == _mapVo.id) { MapTile tile = loadItem.tile; asset.texture.wrapMode = TextureWrapMode.Clamp; // 资源存入缓存 _textureDict[tile] = asset; // 资源保存本地 //App.fileManager.WriteFilePersistentAsync(loader.loader.urlRelative, loader.loader.bytes, false); // 渲染图片 if (_renderTiles.ContainsKey(tile)) { MapBlock block = GetBlock(tile.x - _currentTileX, tile.y - _currentTileY); block.SetTexture(asset.texture); _renderTiles.Remove(tile); if (_loadDict.Contains(tile)) { _loadList.Remove(loadItem); _loadDict.Remove(tile); } } } else { asset.Dispose(); } } // 移除加载字典 if (_loadingDict.ContainsKey(loader.loader.urlRelative)) { _loadingDict.Remove(loader.loader.urlRelative); } _loaderPool.Release(loader); // 继续加载 _loadCount--; LoadNext(); }
/// <summary> /// 创建Asset /// </summary> /// <param name="type"></param> /// <param name="data"></param> /// <param name="assetBundle"></param> /// <param name="bytes"></param> /// <returns></returns> public static IAsset CreateAsset(LoadType type, object data, AssetBundle assetBundle = null, byte[] bytes = null) { IAsset asset; switch (type) { case LoadType.BINARY: asset = new BinaryAsset(); break; case LoadType.BYTEARRAY: asset = new ByteArrayAsset(); break; case LoadType.TEXT: asset = new TxtAsset(); break; case LoadType.JSON: asset = new JSONAsset(); break; case LoadType.XML: asset = new XMLAsset(); break; case LoadType.ASSETBUNDLE: asset = new AssetBundleAsset(); break; case LoadType.TEXTURE: case LoadType.TEXTURE_ASSET_BUNDLE: asset = new TextureAsset(); break; case LoadType.AUDIO: case LoadType.AUDIO_ASSET_BUNDLE: asset = new AudioAsset(); break; case LoadType.ACTION: asset = new ActionAsset(); break; case LoadType.TEXTURE_EXT: asset = new TextureExtAsset(); break; case LoadType.TEXTURE_EXT_SPRITE: asset = new TextureExtSpriteAsset(); break; default: asset = new TxtAsset(); break; } // 将加载器中的资源填充到资源中 asset.asset = data; asset.assetBundle = assetBundle; //asset.bytes = bytes; return(asset); }
/// <summary> /// 渲染 /// </summary> /// <param name="forced"></param> public void Render(bool forced = false) { int startX = (int)(_leftTop.x / _mapVo.tileWidth); int startY = (int)(_leftTop.y / _mapVo.tileHeight); _control.SetBlockPosition(new Vector3(_leftTop.x - _leftTop.x % _mapVo.tileWidth, leftTop.y - _leftTop.y % _mapVo.tileHeight)); // 判断是否需要重绘 if (_currentTileX == startX && _currentTileY == startY && forced == false) { return; } _currentTileX = startX; _currentTileY = startY; // 清理渲染列表 _renderTiles.Clear(); // 计算渲染列表 int maxTileX = startX + _tileX; int maxTileY = startY + _tileY; //int fixedStartX = startX > 0 ? startX : 0; //int fixedStartY = startY > 0 ? startY : 0; for (int y = startY; y < maxTileY; y++) { for (int x = startX; x < maxTileX; x++) { int blockX = x - _currentTileX; int blockY = y - _currentTileY; // block不存在 MapBlock block = GetBlock(blockX, blockY); if (block == null) { continue; } // 坐标超过范围则清空纹理 if (x < 0 || x > _mapVo.maxTileX || y < 0 || y > _mapVo.maxTileY) { block.SetTexture(null); continue; } // 从内存中获取纹理 TextureAsset asset; if (_textureDict.TryGetValue(new MapTile(x, y), out asset)) { // 内存中存在纹理 block.SetTexture(asset.texture); continue; } else { // 内存中不存在纹理 block.SetTexture(null); // 放入加载队列 MapLoaderItem loaderItem = new MapLoaderItem(); loaderItem.mapId = _mapVo.id; loaderItem.tile = new MapTile(x, y); loaderItem.mapX = x * _mapVo.tileWidth; loaderItem.mapY = y * _mapVo.tileHeight; _renderTiles[loaderItem.tile] = loaderItem; } } } if (MapSetting.autoClearTexture) { // 移除缓存 _globalMapTileList.Clear(); foreach (var v in _textureDict) { MapTile tile = v.Key; if (tile.x < startX || tile.x >= maxTileX || tile.y < startY || tile.y >= maxTileY) { _globalMapTileList.Add(tile); } } foreach (MapTile tile in _globalMapTileList) { TextureAsset asset = _textureDict[tile]; _textureDict.Remove(tile); asset.Dispose(); } } // 移除加载队列 for (int i = _loadList.Count - 1; i >= 0; i--) { MapLoaderItem loaderItem = _loadList[i]; if (_renderTiles.ContainsKey(loaderItem.tile) == false) { _loadList.RemoveAt(i); _loadDict.Remove(loaderItem.tile); } } // 显示切片 foreach (var v in _renderTiles) { // 判断是否需要加载 if (_loadDict.Contains(v.Key) == false) { _loadList.Add(v.Value); _loadDict.Add(v.Key); } } // 加载 if (_loadList.Count > 0) { _loadList.Sort(LoadListSortFunc); LoadNext(); } }
/// <summary> /// 从原始数据重创建资源 /// </summary> /// <param name="type"></param> /// <param name="url"></param> /// <param name="assetBundle"></param> /// <param name="bytes"></param> /// <returns></returns> public IAsset CreateAssetFromAssetBundle(LoadType type, string url, AssetBundle assetBundle, byte[] bytes = null) { IAsset asset; string fileName; switch (type) { case LoadType.ASSETBUNDLE: asset = new AssetBundleAsset(); asset.asset = assetBundle; break; case LoadType.TEXTURE_ASSET_BUNDLE: asset = new TextureAsset(); fileName = UrlUtil.GetFileName(url); asset.asset = assetBundle.LoadAsset <Texture2D>(fileName); break; case LoadType.AUDIO_ASSET_BUNDLE: asset = new AudioAsset(); fileName = UrlUtil.GetFileName(url); asset.asset = assetBundle.LoadAsset <AudioClip>(fileName); break; case LoadType.ACTION: asset = new ActionAsset(); fileName = UrlUtil.GetFileName(url); ActionData actionData = App.objectPoolManager.GetObject <ActionData>(); actionData.Init(fileName, assetBundle); asset.asset = actionData; break; case LoadType.TEXTURE_EXT: asset = new TextureExtAsset(); fileName = UrlUtil.GetFileName(url); TextureExt textureExt = App.objectPoolManager.GetObject <TextureExt>(); textureExt.Init(fileName, assetBundle); asset.asset = textureExt; break; case LoadType.TEXTURE_EXT_SPRITE: asset = new TextureExtSpriteAsset(); fileName = UrlUtil.GetFileName(url); TextureExtSprite textureExtSprite = App.objectPoolManager.GetObject <TextureExtSprite>(); textureExtSprite.Init(fileName, assetBundle); asset.asset = textureExtSprite; break; default: asset = null; break; } if (asset == null) { return(null); } asset.assetBundle = assetBundle; //asset.bytes = bytes; return(asset); }