ResourceManager.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /** @format */
  2. import {Data, Mgr} from '../GameControl'
  3. import {Log} from '../utils/LogUtils'
  4. /** @format */
  5. export class ResourceManager {
  6. public init() {
  7. if (Mgr.platform.isWeChat()) {
  8. // @ts-ignore
  9. wx.onMemoryWarning(() => {
  10. Log.warn('MemoryWarning!start gc')
  11. Mgr.platform.gc()
  12. })
  13. }
  14. }
  15. public dumpSize() {
  16. cc.assetManager.assets.forEach((value: cc.Asset, key) => {
  17. console.log(value, key)
  18. })
  19. }
  20. async loadTexSync(url: string) {
  21. let sf = await this.loadSync(Data.main.texBundle, url, cc.SpriteFrame)
  22. return sf
  23. }
  24. //同步加载
  25. public loadSync<T extends cc.Asset>(bundle: cc.AssetManager.Bundle, url: string, type: typeof cc.Asset) {
  26. return new Promise((resolve: (asset: T) => void, reject) => {
  27. bundle.load(url, type, (err, asset: T) => {
  28. if (err) {
  29. resolve(null)
  30. } else {
  31. resolve(asset)
  32. }
  33. })
  34. })
  35. }
  36. //同步加载
  37. public loadDirSync<T extends cc.Asset>(bundle: cc.AssetManager.Bundle, url: string, type: typeof cc.Asset) {
  38. return new Promise((resolve: (asset: T[]) => void, reject) => {
  39. bundle.loadDir(url, type, (err, asset: T[]) => {
  40. if (err) {
  41. resolve(null)
  42. } else {
  43. resolve(asset)
  44. }
  45. })
  46. })
  47. }
  48. }