ResourcesUtils.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { Asset, assetManager, AssetManager, Component, Node, resources, SceneAsset, __private } from "cc";
  2. /**
  3. * 资源工具,resources的api全部await化
  4. * @author 袁浩
  5. */
  6. export class ResourcesUtils {
  7. /**
  8. * 加载本地资源,使用说明见resources.load
  9. * @returns await 加载成功后返回的数据
  10. */
  11. public static load<T extends Asset>(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType, component: Component | Node, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  12. return new Promise<T>((resolve: (data: T) => void, reject) => {
  13. resources.load(paths, type, onProgress, (err: Error = null, data: T): void => {
  14. if (!err) {
  15. if (component && !component.isValid) {
  16. return;
  17. }
  18. resolve(data);
  19. }
  20. else {
  21. reject(err);
  22. }
  23. });
  24. });
  25. }
  26. /**
  27. * 加载远程资源,使用说明见assetManager.loadRemote
  28. * @returns await 加载成功后返回的数据
  29. */
  30. public static loadRemote<T extends Asset>(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, component: Component, onComplete?: __private.cocos_core_asset_manager_shared_CompleteCallbackWithData<T> | null) {
  31. return new Promise<T>((resolve: (data: T) => void, reject) => {
  32. assetManager.loadRemote(url, options, (err: Error = null, data: T): void => {
  33. if (!err) {
  34. if (component && !component.isValid) {
  35. return;
  36. }
  37. resolve(data);
  38. }
  39. else {
  40. reject(err);
  41. }
  42. });
  43. });
  44. }
  45. /**
  46. * 使用说明见resources.preload
  47. * @returns await 加载成功后返回的数据
  48. */
  49. public static preload(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  50. return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
  51. resources.preload(paths, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
  52. if (!err) {
  53. resolve(data);
  54. }
  55. else {
  56. reject(err);
  57. }
  58. });
  59. });
  60. }
  61. /**
  62. * 使用说明见resources.loadDir
  63. * @returns await 加载成功后返回的数据
  64. */
  65. public static loadDir<T extends Asset>(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType<T> = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  66. return new Promise<T>((resolve: (data: T) => void, reject) => {
  67. resources.loadDir(dir, type, onProgress, (err: Error = null, data: T | any): void => {
  68. if (!err) {
  69. resolve(data);
  70. }
  71. else {
  72. reject(err);
  73. }
  74. });
  75. });
  76. }
  77. /**
  78. * 使用说明见resources.preloadDir
  79. * @returns await 加载成功后返回的数据
  80. */
  81. public static preloadDir(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  82. return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
  83. resources.preloadDir(dir, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
  84. if (!err) {
  85. resolve(data);
  86. }
  87. else {
  88. reject(err);
  89. }
  90. });
  91. });
  92. }
  93. /**
  94. * 使用说明见resources.loadScene
  95. * @returns await 加载成功后返回的数据
  96. */
  97. public static loadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  98. return new Promise<any>((resolve: (data: any) => void, reject) => {
  99. resources.loadScene(sceneName, options, onProgress, (err: Error = null, data: SceneAsset): void => {
  100. if (!err) {
  101. resolve(data);
  102. }
  103. else {
  104. reject(err);
  105. }
  106. });
  107. });
  108. }
  109. /**
  110. * 使用说明见resources.preloadScene
  111. * @returns await 加载成功后返回的数据
  112. */
  113. public preloadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback) {
  114. return new Promise<void>((resolve: () => void, reject) => {
  115. resources.preloadScene(sceneName, options, onProgress, (err: Error = null): void => {
  116. if (!err) {
  117. resolve();
  118. }
  119. else {
  120. reject(err);
  121. }
  122. });
  123. });
  124. }
  125. }