ResourceLoader.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { _decorator, Component, __private, AssetManager, resources, Asset, assetManager, SceneAsset } from "cc";
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 资源加载组件
  5. * @author 袁浩
  6. */
  7. @ccclass('ResourceLoader')
  8. export class ResourceLoader extends Component {
  9. /**
  10. * 加载本地资源,使用说明见resources.load
  11. * @returns await 加载成功后返回的数据
  12. */
  13. public load<T extends Asset>(paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  14. return new Promise<T>((resolve: (data: T) => void, reject) => {
  15. resources.load(paths as any, type, onProgress, (err: Error = null, data: T): void => {
  16. if (!err) {
  17. if (!this.node || !this.node.isValid) {
  18. return;
  19. }
  20. resolve(data);
  21. }
  22. else {
  23. reject(err);
  24. }
  25. });
  26. });
  27. }
  28. /**
  29. * 加载远程资源,使用说明见assetManager.loadRemote
  30. * @returns await 加载成功后返回的数据
  31. */
  32. public loadRemote<T extends Asset>(url: string, options: __private.cocos_core_asset_manager_shared_IRemoteOptions, onComplete?: __private.cocos_core_asset_manager_shared_CompleteCallbackWithData<T> | null) {
  33. return new Promise<T>((resolve: (data: T) => void, reject) => {
  34. assetManager.loadRemote(url, options, (err: Error = null, data: T): void => {
  35. if (!err) {
  36. if (!this.node.isValid) {
  37. return;
  38. }
  39. resolve(data);
  40. }
  41. else {
  42. reject(err);
  43. }
  44. });
  45. });
  46. }
  47. /**
  48. * 使用说明见resources.preload
  49. * @returns await 加载成功后返回的数据
  50. */
  51. public preload(paths: string | any, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  52. return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
  53. resources.preload(paths, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
  54. if (!err) {
  55. resolve(data);
  56. }
  57. else {
  58. reject(err);
  59. }
  60. });
  61. });
  62. }
  63. /**
  64. * 使用说明见resources.loadDir
  65. * @returns await 加载成功后返回的数据
  66. */
  67. public 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) {
  68. return new Promise<T>((resolve: (data: T) => void, reject) => {
  69. resources.loadDir(dir, type, onProgress, (err: Error = null, data: T | any): void => {
  70. if (!err) {
  71. resolve(data);
  72. }
  73. else {
  74. reject(err);
  75. }
  76. });
  77. });
  78. }
  79. /**
  80. * 使用说明见resources.preloadDir
  81. * @returns await 加载成功后返回的数据
  82. */
  83. public preloadDir(dir: string, type: __private.cocos_core_asset_manager_shared_AssetType = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  84. return new Promise<AssetManager.RequestItem[]>((resolve: (data: AssetManager.RequestItem[]) => void, reject) => {
  85. resources.preloadDir(dir, type, onProgress, (err: Error = null, data: AssetManager.RequestItem[]): void => {
  86. if (!err) {
  87. resolve(data);
  88. }
  89. else {
  90. reject(err);
  91. }
  92. });
  93. });
  94. }
  95. /**
  96. * 使用说明见resources.loadScene
  97. * @returns await 加载成功后返回的数据
  98. */
  99. public loadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  100. return new Promise<any>((resolve: (data: any) => void, reject) => {
  101. resources.loadScene(sceneName, options, onProgress, (err: Error = null, data: SceneAsset): void => {
  102. if (!err) {
  103. resolve(data);
  104. }
  105. else {
  106. reject(err);
  107. }
  108. });
  109. });
  110. }
  111. /**
  112. * 使用说明见resources.preloadScene
  113. * @returns await 加载成功后返回的数据
  114. */
  115. public preloadScene(sceneName: string, options: __private.cocos_core_asset_manager_shared_IAssetOptions = null, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback) {
  116. return new Promise<void>((resolve: () => void, reject) => {
  117. resources.preloadScene(sceneName, options, onProgress, (err: Error = null): void => {
  118. if (!err) {
  119. resolve();
  120. }
  121. else {
  122. reject(err);
  123. }
  124. });
  125. });
  126. }
  127. /**
  128. * 加载资源包
  129. * @param nameOrUrl 资源包名字或者地址
  130. * @returns 资源包对象
  131. */
  132. public loadBundle(nameOrUrl: string) {
  133. return new Promise<AssetManager.Bundle>((resolve: (data: AssetManager.Bundle) => void, reject) => {
  134. assetManager.loadBundle(nameOrUrl, (err: Error | null, data: AssetManager.Bundle) => {
  135. if (!err) {
  136. resolve(data);
  137. }
  138. else {
  139. reject(err);
  140. }
  141. });
  142. });
  143. }
  144. /**
  145. * 加载资源包中的资源
  146. * @param bundle
  147. * @param paths
  148. * @param type
  149. * @param onProgress
  150. * @returns
  151. */
  152. public loadInBundle<T extends Asset>(bundle: AssetManager.Bundle, paths: string[] | string, type: __private.cocos_core_asset_manager_shared_AssetType, onProgress: __private.cocos_core_asset_manager_shared_ProgressCallback = null) {
  153. return new Promise<T>((resolve: (data: T) => void, reject) => {
  154. bundle.load(paths as any, type, onProgress, (err: Error = null, data: T) => {
  155. if (!err) {
  156. resolve(data);
  157. }
  158. else {
  159. reject(err);
  160. }
  161. });
  162. });
  163. }
  164. }