//异步加载图片 会自动识别图集:回调方式(image 和button已经封装 外部使用时候 谨慎使用) public static async ETTask <Sprite> LoadImageAsync(this ImageLoaderComponent self, string image_path, Action <Sprite> callback = null) { Sprite res = null; CoroutineLock coroutineLock = null; try { coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Resources, image_path.GetHashCode()); self.__GetSpriteLoadInfoByPath(image_path, out int asset_type, out string asset_address, out string subasset_name); if (asset_type == ImageLoaderComponent.SpriteType.Sprite) { res = await self.__LoadSingleImageAsyncInternal(asset_address, callback); } if (asset_type == ImageLoaderComponent.SpriteType.DynSpriteAtlas) { res = await self.__LoadDynSpriteImageAsyncInternal(asset_address, callback); } else { res = await self.__LoadSpriteImageAsyncInternal(asset_address, subasset_name, callback); } } catch (Exception ex) { Log.Error(ex); } finally { coroutineLock?.Dispose(); } return(res); }
//释放图片 public static void ReleaseImage(this ImageLoaderComponent self, string image_path) { if (string.IsNullOrEmpty(image_path)) { return; } self.__GetSpriteLoadInfoByPath(image_path, out int asset_type, out string asset_address, out string subasset_name); if (asset_type == ImageLoaderComponent.SpriteType.SpriteAtlas) { if (self.m_cacheSpriteAtlas.TryOnlyGet(image_path, out SpriteAtlasValue value)) { if (value.ref_count > 0) { var subasset = value.subasset; if (subasset.ContainsKey(subasset_name)) { subasset[subasset_name].ref_count = subasset[subasset_name].ref_count - 1; if (subasset[subasset_name].ref_count <= 0) { GameObject.Destroy(subasset[subasset_name].asset); subasset.Remove(subasset_name); } value.ref_count = value.ref_count - 1; } } } } else if (asset_type == ImageLoaderComponent.SpriteType.DynSpriteAtlas) { var index = asset_address.IndexOf(self.DYN_ATLAS_KEY); var path = asset_address.Substring(0, index); if (self.m_cacheDynamicAtlas.TryGetValue(path, out var value)) { value.RemoveTexture(image_path, true); } } else { if (self.m_cacheSingleSprite.TryOnlyGet(image_path, out SpriteValue value)) { if (value.ref_count > 0) { value.ref_count = value.ref_count - 1; } } } }